The other day one of my colleagues asked me whether do we have any functions inside flex that can encode a URL . Even though it quite obvious that something can be done manually to replace the strings, but in order to find some answers I looked into the Top level packages where I found encodeURI() and decodeURI() . What does these functions actually do?
This function bascially encodes the string into a valid URI. The following shown below are not encoded by encodeURI() unless it is a small group of basic characters as given in the documentation.
|Characters that are not encoded|
There is another encoding function that is slightly different from encodeURI() – encodeURIComponent() which also does the same encoding of strings to URI but.Before we talk about the major difference we should be aware of some terms :
URIComponent: is the text that appears in between any special characters..for eg: http and http://www.google.com are the URI components in http://www.google.com.
Component Seperators: are the special characters used in a URI (: ; / ?)
Now coming back to the major difference the encodeURIComponent() in encodes the same as encodeURI() and additionally also encodes special characters (; / ? : @ & + $ , # )
This function does the exact reverse of encodeURI() , it decodes the encoded URI into string where all encoded characters are restored into unencoded representation.
|Escape sequence not decoded||Characters equivalents|
Using the decodeURIComponent() the escape sequence in the above table can be decoded.
All the above functions are in addition to the already existing escape() and unescape() functions which were also used in flash.escape() encodes all nonalphanumeric charachters into format % and hexadecimal sequence and unescape() decodes the URL encoded format to string.
So , there are plenty of things to play with when you need some strings to be encoded, each function does look similar but it has some variations which needs to be closely observed.Before concluding here is a small code snippets which includes the usage of above functions:
var redirectUrl:String = "http://www.somedomain.com?loggedin=true& username=Sun";
var escaped:String = escape(redirectUrl);
var unescaped:String = unescape(escaped);
var encoded:String = encodeURI(redirectUrl);
var decoded:String = decodeURI(encoded);
var encodedURI:String = encodeURIComponent(redirectUrl);
var decodedURI:String = decodeURIComponent(encodedURI);
trace("escaped: "+escaped); //escaped: http%3A//www.somedomain.com%3Floggedin%3Dtrue%26username%3DSun
trace("unescaped: "+unescaped); //unescaped: http://www.somedomain.com?loggedin=true&username=Sun
trace("encoded: "+encoded); //encoded: http://www.somedomain.com?loggedin=true&%20username=Sun
trace("decoded: "+decoded); //decoded: http://www.somedomain.com?loggedin=true&username=Sun
trace("encodedURI: "+encodedURI); //encodedURI: http%3A%2F%2Fwww.somedomain.com%3Floggedin%3Dtrue%26username%3DSun
trace("decodedURI: "+decodedURI); //decodedURI: http://www.somedomain.com?loggedin=true&username=Sun
Meanwhile when I was googling more on this, I found out a bug reported to Adobe where it was reported that encodeURI and decodeURI misbehaves with httpservice which is mentioned to be deferred and will be fixed in 4.0 hmm..I am curious to know about your experiences when using these functions…