jedierikb
jedierikb

Reputation: 13079

escaping characters in as3 as javascript does

I am having trouble escaping special characters in as3.

trace( escape("who are ü?") );

returns who%20are%20%uFFFD%3F

or

trace( encodeURIComponent("who are ü?") );

returns who%20are%20%EF%BF%BD%3F

while in javascript this

alert( encodeURIComponent("who are ü?") );

returns who%20are%20%C3%BC%3F

and

alert( escape("who are ü?") );

returns who%20are%20%FC%3F

Any suggestions how to get as3 to return escaped special characters as javascript is? Why is as3 apparently choking?

(here is a good reference: http://www.ultrashock.com/forums/actionscript/as3-escape-vs-as2-escape-122046.html )

Upvotes: 2

Views: 13440

Answers (2)

troelskn
troelskn

Reputation: 117487

Looks like AS is escaping the string as UTF-16, while the Javascript example is escaping as UTF-8. escape doesn't define how to deal with non-ascii characters, and so it's been deprecated as of JavaScript 1.5. You should use the function encodeURIComponent instead, which is defined as escaping as UTF-8 - This should be consistent across different implementations. If you require the AS behaviour, I don't think there is a native function in Javascript, but you can use the functions provided here.

Upvotes: 5

jedierikb
jedierikb

Reputation: 13079

Although my IDE (intellij) was displaying, saving, and loading special characters, they were being saved in 1252. Switching to UTF-8 has fixed this.

Upvotes: 1

Related Questions