views:

211

answers:

1

Hi,

I'm trying to make some "social share" buttons at my site, but the urls I generate just don't get decoded by this services.

One example, for twitter:

private function twitter(e:Event):void {
  var message:String = "Message with special chars âõáà";
  var url:String = "http://www.twitter.com/home?status=";
  var link:URLRequest = new URLRequest( url + escape(message) );
}

But when twitter opens up, the message is:

Message with special chars %E2%F5%E1%E0

Something similar is happening with Facebook and Orkut (but these two hide the special chars).

Someone know why is this happening?

A: 

The problem is that the escape() function doesn't take UTF-8 enconding into account. The function you want for encoding the querystring using UTF-8 is encodeURIComponent().

So, let's say you have an "ñ" (eñe in Spanish, or n plus tilde). I'm using "ñ", because I remember both its code point and its UTF-8 representation, since I always use it for debugging, but the same applies for other non-ASCII, non-alphanumeric number.

Say you have the string "Año" ("year" in Spanish, by the way).

The code points (both in Unicode and iso-8859-1) are:

A: 0x41
ñ: 0xf1
o: 0x6f

If you call escape(), you'll get this:

A: A
ñ: %F1
o: o

"A" and "o" don't need to be encoded. The "ñ" is encoded as "%" plus its code point, which is 0xf1.

But, twitter, facebook, etc, expect UTF-8. 0xf1 is not a valid UTF-8 sequence and should be represented with a 2 bytes sequence. Meaning, "ñ" should be encoded as:

0xC3
0xB1

This is what encodeURIComponent does. It will encode "año" this way:

A: A
ñ: %C3
   %B1
o: o

So, to sum up, instead of this:

 var link:URLRequest = new URLRequest( url + escape(message) );

try this

 var link:URLRequest = new URLRequest( url + encodeURIComponent(message) );

And it should work fine.

Juan Pablo Califano
Thanks, this really helped me out with facebook and orkut, but twitter still shows my message as:Message%20with%20special%20chars%20%C3%A2%C3%B5%C3%A1%C3%A0now displaying the chars in UTF-8 encoding.
Rittmeyer
I found a solution for this other issue. Seems like you have to use http://twitter.com/ instead of http://www.twitter.com/
Rittmeyer
Mmmm, that's strange. I've used this code for posting updates to twitter and have not had problems.
Juan Pablo Califano
Ah, ok. Glad you solved your problem.
Juan Pablo Califano