How to encode URL in JavaScript?

By | August 8, 2020
If you are looking to encode an URL that have an query string (like '?name=akhilesh shukla') on one of your page and you don't want your visitor's to see that or in some case like you want to redirect your visitor to a link where the link accept parameters in encoded format then you must have to encode the parameter before redirecting user to that particular link.

Encode URL using encodeURIComponent() Method:

var param = 'Akhilesh Shukla #fhfd 547';
var linkURL = 
 "http://example.com/index.php?url=" + encodeURIComponent(param);

In above example, I have encoded the param variable. Though, encoding URL can also be done using encodeURI() method. However, encodeURI() does not encode special characters like #,/,? etc.

Recommended article: How to remove query string parameter using jQuery
 

Leave a Reply

Your email address will not be published. Required fields are marked *