Tuesday, November 18, 2014

encodeURI() and encodeURIComponent() in JavaScript.

In JavaScript, we have two utilities for encoding an URI:

encodeURI():
1) normally for encoding a full URI (given that parameters are properly encoded already)
2) encodes everything except [a-A0-9] with ~!@#$&*()=:/,;?+'

encodeURIComponent():
1) normally for encoding URI's "component" such as parameters' value in the query part.
2) encodes everything except [a-A0-9] with ~!*()'
 
1. Reserved characters in URI: http://en.wikipedia.org/wiki/Percent-encoding#Types_of_URI_characters
2. Nice article on the comparison of the two functions: http://xkr.us/articles/javascript/encode-compare/
 

No comments:

Post a Comment

(Coding && Eating) || Sleeping