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

1 Answer

+1 vote
by (4.4k points)

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 = "http://example.com/index.html?param1=1&param2=2";
        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>

    <h1>URL: http://example.com/index.html?param1=1&amp;param2=2</h1>
    <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


Share:- Whatsapp Facebook Facebook

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