Compare escape(), encodeURI(), and encodeURIComponent()


escape() Method

The escape() function computes a new string in Unicode format where certain characters have been replaced by a hexadecimal escape sequence. It encodes all the spaces, punctuation, accented characters, and any other non-ASCII characters. It doesn't encode all the special characters, such as @*_+-./. It replaces characters with %xx encoding, where xx is equivalent to the hexadecimal number representing the character. For example, # is returned as %23.
Test characters: ~!@#$%^&*=+(){}[],?/:;'\"\\

encodeURI() Method

The encodeURI() method returns an encoded URI (Uniform Resource Identifier) by replacing each instance of certain characters by one, two, three, or four escape sequences. The method does not encode characters that are reserved for a URI. Characters, such as ~!@#$&*()=:/,;?+' doesn't get encoded. However, encodeURIComponent() can be used to encode these characters.
Test characters: ~!@#$%^&*=+(){}[],?/:;'\"\\

encodeURIComponent() Method

This is THE BEST option to get job done in complete fashion. The encodeURIComponent() method encoded as a valid component of a Uniform Resource Identifier (URI). Passing result to decodeURIComponent() returns the original string. This is a complete encoder means this method encodes every single characters (except ~!*()'). To keep in mind, it encodes /,so we have to be careful if the string is a path such as /user/folder/index.html. Since all characters get encoded, the slash characters will be encoded too and the path will not be valid if sent as a request to a web server. It is better to use the encodeURI() method if the string contains more than a single URI component. However, its more aggressive than encodeURI() since it encodes any potential harmful characters.
Test characters: ~!@#$%^&*=+(){}[],?/:;'\"\\

URI ENCODER

Encode your URI using all the methods mentioned above_
[ Outputs will be the encoded values of test string unless value is provided ]
Test characters: ~!@#$%^&*=+(){}[],?/:;'\"\\

escape():

encodeURI():

encodeURIComponent() (recommended):

Check out Compare unescape(), decodeURI(), and decodeURIComponent()

No comments:

Post a Comment

Grab The Button...

Grab the above button and add to your blog or website to get easy access to Virtual Species. Simply click the textbox, copy the code and paste where you want to see the button. That's it!