Encode URL in Javascript

You have three options

OP1:

escape(str) will not encode: * @ - _ + . /

Example:

escape("http://example.com/page.html?params=@@@/")

Result:

http%3A//example.com/page.html%3Fparams%3D@@@/


OP2:

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

Example:

encodeURI("http://example.com/page.html?params=@@ @/")

Result:

http://example.com/page.html?params=@@%20@/


OP3:

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

Example:

encodeURIComponent("http://example.com/page.html?params=@@ @/")

Result:

http%3A%2F%2Fexample.com%2Fpage.html%3Fparams%3D%40%40%20%40%2F


Decode URL in Javascript

You have three options

OP1:

unescape(str_esc)

Example:

unescape("http%3A//example.com/page.html%3Fparams%3D@@@/")

Result:

http://example.com/page.html?params=@@@/


OP2:

decodeURI(uri_enc)

Example:

decodeURI("http://example.com/page.html?params=@@%20@/")

Result:

http://example.com/page.html?params=@@ @/


OP3:

decodeURIComponent(uri_enc)

Example:

decodeURIComponent("http%3A%2F%2Fexample.com%2Fpage.html%3Fparams%3D%40%40%20%40%2F")

Result:

http://example.com/page.html?params=@@ @/