Share:- Whatsapp Facebook Facebook

------------------------------------- Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.


0 votes
How do you encode URL in JavaScript?
I want to encode above url. Please help me to encode url.
in JavaScript by

1 Answer

+1 vote

Javascript provides us two method like: encodeURI() and encodeURIComponent().

The encodeURI() function is used to encode a URI. This function encodes special characters, except: , / ? : @ & = + $ # (Use encodeURIComponent() to encode these characters).


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<script language="javascript" type="text/javascript">
    function doEncode(){
        var myUrl = "";
        var endcodedUurl = encodeURIComponent(myUrl);
        document.getElementById('encodeURLDiv').innerHTML = endcodedUurl;
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Java script encode url example</title>

    <a href="" onClick="doEncode(); return false;">Encode URL</a>
    <h2>Encode URL: <div id="encodeURLDiv"></div></h2>

Output:-  Before output of URL Encode

Before url encode

After encode see the output of encoded url:

Encoded url


by (3.1k points)