JavaScript

How To Encode a URL With JavaScript

You can use the encodeURIComponent() method to safely encode a URL in JavaScript.

The encodeURIComponent() method encodes all characters. In addition, it encodes the following characters:

, /? : @ & = + $ #

 
Let’s see an example to know how this method works:

var url = 'https://stackhowto.com/search?q='
var req = 'Welcöm tö St@ckHöwTö'

// encode only the string 'req'
var fullUrl = url + encodeURIComponent(req)
console.log(fullUrl)

Output:

https://stackhowto.com/search?q=Welc%C3%B6m%20t%C3%B6%20St%40ckH%C3%B6wT%C3%B6
Note: You should not encode the entire URL using the encodeURIComponent() function. It should only be used to encode the url parameters.
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

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