Sunday 29 March 2015

Difference between encodeuri and encodeuricomponent in javascript?

Difference between encodeuri and encodeuricomponent in javascript

encodeURI: This javascript function is used to encode a URI (Uniform Resource Identifier). This function encode special characters except the following:
, / ? : @ & = + $ #

We used encodeURI when we want to encode the url parameter.
For Example:
var url=encodeURI('http://www.example.com/mysite.php?name=web technology experts notes&char=*');
console.log(url);
//output will be following
//http://www.example.com/mysite.php?name=web%20technology%20experts%20notes&char=*

decodeURI(): It is used to decode a URL which was encoded with encodeURI.




encodeURIComponent: This javascript function is used to encode a URI component. This function encodes all special characters Except the following:
~!*()'

We used encodeURIComponent when we want to encode the both (URI parameter & URI components). For Example:
var url=encodeURIComponent('http://www.example.com/mysite.php?name=web technology experts notes&char=*');
console.log(url);
//output will be following
//http%3A%2F%2Fwww.example.com%2Fmysite.php%3Fname%3Dweb%20technology%20experts%20notes%26char%3D*
decodeURIComponent() It is used to decode a URL which was encoded with encodeURIComponent.