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 = ''
var req = 'Welcöm tö St@ckHöwTö'

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

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 *