JavaScript provides an encodeURIComponent() function to encode uri components. This function encrypt the special characters provided in the url such as “space , / ? : @ & = + $ #“.
Syntax
encodeURIComponent(uri)
Example
var uri="codesolution.org/calculate-time-between-two-dates/"; var encodeduri=encodeURIComponent(uri); $("#uri").html(uri); $("#encodeduri").html(encodeduri);
Html
<h2> How to encode URI components in javascript? </h2> <b>URI:</b> <div id=uri></div> <b>Encoded URI:</b> <div id="encodeduri"></div>
Result
How to encode URI components in javascript?
URI:
codesolution.org/calculate-time-between-two-dates/
Encoded URI:
codesolution.org%2Fcalculate-time-between-two-dates%2F
Try it