How to encode URL in JavaScript?

How can we encode URL in javascript same like in PHP we do with urlencode().

When you are working to encode url in javascript you have following functions for encoding purpose. 

encodeURI() will not encode: ~!@#$&*()=:/,;?+'

encodeURIComponent() will not encode: ~!*()'


1- encodeURIComponent() is used when you want to encode query parameters in URL like

var url = "" + encodeURIComponent(somevalue) + "¶m2=100";


2- You need to encodeURI() if you want a working URL:

encodeURI(" file with spaces.html");


Hope you find it useful.