Difference between encodeURI and encodeURIComponent in javascript

by Guna 2014-05-20 15:26:31

You can use encodeURI function to encode a url: http://www.google.com/so many spaces.html

encodeURI  will encode this as,
 
http://www.google.com/so%20many%20spaces.html

You have another option to encode an url, i.e. encodeURIComponent. But, this will encode above url as,
 
http%3A%2F%2Fwww.google.com%2Fso%20many%20spaces.html

encodeURIComponent function will comes handy when we need to encode a full url that is going 
to be sent as parameter, ex:

param1 = encodeURIComponent("http://xyz.com/?a=12&b=55");
url = "http://domain.com/?param1=" + param1 + "param2=99";
http://www.domain.com/?param1=http%3A%2F%2Fxyz.com%2F%Ffa%3D12%26b%3D55&param2=99

 
740
like
0
dislike
0
mail
flag

You must LOGIN to add comments