开发者

Actionscript problems with social share encoding

开发者 https://www.devze.com 2022-12-25 22:28 出处:网络
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.

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)开发者_如何学Python );
}

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号