Friday, March 4, 2011

The difference between escape(), encodeURI(), and encodeURIComponent()...

http://xkr.us/articles/javascript/encode-compare/

escape: convert non-ASCII chars to hex string encoding

encodeURI:
Use of the encodeURI() method is a bit more specialized than escape() in that it encodes for URIs [REF] as opposed to the querystring, which is part of a URL. Use this method when you need to encode a string to be used for any resource that uses URIs and needs certain characters to remain un-encoded. Note that this method does not encode the ' character, as it is a valid character within URIs.

encodeURIComponent: encodes everything

decodeURIComponent: converts back

http://www.diveintojavascript.com/core-javascript-reference/the-decodeuricomponent-function

The common way to UTF-8 encode a string is:
unescape(encodeURIComponent(mystring));

How does this work? If we simply did an escape(mystring), then all the spaces would get replaced by %20. We could use encodeURIComponent() to first convert the entire string, but doing a decodeURIComponent would get us back to where we started. To leverage the UTF-8 encoding aspects of URIComponent, we use encodeURIComponent and then use unescape().

http://www.javascripter.net/faq/unescape.htm

Answer: To convert a string from URL-encoded form, use the JavaScript function unescape(string). This function works as follows: if the string contains escape-sequences of the form %XX, where XX stands for two hexadecimal digits, each escape-sequence is replaced by the character whose ASCII code is XX. Otherwise, the string remains unchanged.

(In Unicode-aware browsers, in addition to escape-sequences %XX, the unescape function also processes sequences of the form %uXXXX; see also escape.)

No comments:

Post a Comment