Forum Moderators: open

Message Too Old, No Replies

document.title and article title %20 bit.ly problem

I don't want %20 sent to Twitter Status

         

Meancode

1:03 am on Aug 7, 2009 (gmt 0)

10+ Year Member



I am quite new to the API and found a script that works, but it puts in %20 for spaces in the title if I use escape(document.title) or code for Movable Type for the article title, <$MTEntryTitle encode_html="0">. So how do I get rid of the %20 in the resulting tweet? The resulting code has spaces in the Entry title, but when it gets to the Twitter Status box, it has %20, for the spaces of course.

Also it does not matter if I use <$MTEntryTitle encode_html="0">, <$MTEntryTitle encode_html="1"> or escape(document.title).

Here is the code I am using:

<script type="text/javascript" charset="utf-8" src="http://bit.ly/
javascript-api.js?version=latest&login=LOGIN&apiKey=API_KEY"></script>
<script type="text/javascript">
BitlyCB.shortenResponse = function(data) {
var s = '';
var first_result;
// Results are keyed by longUrl, so we need to grab the first one.
for (var r in data.results) {
first_result = data.results[r]; break;

}

// Now get the shortURL
shortURL = first_result["shortUrl"];
document.location = "http://www.twitter.com/home/?status=<$MTEntryTitle encode_html="0">+" + shortURL + "+(via+@example)";
}

</script>
<img src="/images/tweet_this.gif" onClick="BitlyClient.shorten
(document.location, 'BitlyCB.shortenResponse');">

whoisgregg

2:59 pm on Aug 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the actual source code does it show %20 in the spaces? When I send a status update to Twitter in the same manner you are using, Twitter correctly decodes %20 characters into spaces.

After running the escape(document.title), you could do a .replace() to turn %20 into a plus sign. That should work (of course, so should the %20, so I suspect there's more going wrong here).

Meancode

4:45 pm on Aug 7, 2009 (gmt 0)

10+ Year Member



In the source code it shows no %20. When the result gets put into the Twitter Status window, the %20 show up.

This seems to work:

document.location = "http://www.twitter.com/home/?status=Reading:+" + document.title.replace(/ /g,"+") + "+" + shortURL + "+(via+@example)";

However it was just dying when it got to twitter.com. I was not sure if it was because Twitter was being hammered by the DOS attack yesterday or what. I figured it out. My Titles have a space hyphen space in them, creating +-+ in the URL, and it does not like that.

When I click the image, it cannot load Twitter at all. It just sits there and spins, Firefox and Safari. In Safari I am getting the "Safari can't open page" and this is the URL that the script makes:

[twitter.com...]

Though if I just hit return in the URL field, it loads Twitter, places the messages into the Status window, and it is ready to post, right away (in Safari, not Firefox).

I don't get that.

I will have to test it on other computers. But you can test it here:

<snipped url>

[edited by: whoisgregg at 5:16 pm (utc) on Aug. 7, 2009]
[edit reason] Whoops, no URLs please. See TOS [webmasterworld.com] :) [/edit]

whoisgregg

5:18 pm on Aug 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm thinking the issue is either browser or DoS related. The link you posted there works correctly for me in Safari Version 4.0.2 (5530.19).

Meancode

7:28 am on Aug 8, 2009 (gmt 0)

10+ Year Member



Yea and it works perfectly now in Firefox for me. It must have been the DoS that was happening. Glad to hear that!

Sorry, I did not realize the no URLs rule pertained to giving people examples of live code, which isn't that basically the point of coming here to get help?

Ken

whoisgregg

9:32 pm on Aug 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey Ken,

The problem with linking to test code/broken code is that those pages tend to get either fixed or pulled down at some point. So someone else who comes along a few weeks from now with the same problem only sees half the details.

By including relevant code snippets in the thread, we make this a much more useful place for all of us.

Glad you got it sorted. :)

Meancode

9:38 am on Aug 10, 2009 (gmt 0)

10+ Year Member



That makes sense.