ActionScript URL Encoding/Decoding

Posted By : todd sharp Posted At : January 11, 2007 8:37 AM Posted In: ActionScript, ColdFusion

7

I just had an issue on one of my work sites where a flash form was passing a dynamic data element via the url from a grid to a new page which pulled a report. The problem was that one of the dynamic values contained a "&" which obviously presents a problem. If this was pure CF I could simply use urlEncodedFormat() and urlDecode() to handle this, but since the variable in question is coming from a flash form I wasn't quite sure how to handle this one (Flash gurus can look away now).

A quick Google later, I found that ActionScript has a very similar set of functions called escape() and unescape(). I only needed to use escape() in my case, since the var was being handled by CF on the target page. I did employ the good old urlDecode() on the target page and everything worked out perfectly. Don't you love it when the solution is so simple? It's almost like the folks that make these languages know what they're doing.

Comments (7)

Ameen's Gravatar This function exactly like JavaScript escape() function that's encodes/formats the url before using it as request url of ajax dynamic call. It's very nice function, in some times it solves my purpose but other times it removes any non-ascii characters from the url. may be I'm using it in wrong way.


Ameen

Paul's Gravatar One quick search brought me here...
I chose not to do XML but sendvars and encountered
the same thing. Gonna go check escape() out thanks!

plumento's Gravatar Additional Information:

The unescape method in AS 3 has been changed from AS 2. If you want to unescape + (urlencoded whitespace) you have to use the replace method in combination.

var urlencodedString:String = "foo+bar%26param=1";

var badString = unescape(urlencodedString);

var niceString:String = unescape(urlencodedString).replace(/\+/g, " ");

trace(badString) // foo+bar&param=1
trace(niceString) // foo bar&param=1

plumento's Gravatar Correction:

var badString:String = unescape(urlencodedString);

max's Gravatar You should also see decodeURI() and decodeURIComponent():

http://livedocs.adobe.com/flash/9.0/ActionScriptLa...()

http://livedocs.adobe.com/flash/9.0/ActionScriptLa...Component()

derek's Gravatar Thanks for your post. It really helps. I was trying to fatch html data via loadVar function and got hexadecimal sequences. With the unescape function
I could unvail the original html code in flash.

coach bags outlet's Gravatar I really like your article, I support your point of view