JavaScript > Global Reference

Decoding URI in JavaScript

How to decode a encoded uniform resource identifier (URI) in JavaScript?


By using decodeURI() we can decode a encoded uniform resource idnetifier (URI).

<p>Click the below button to Decode after Encode.</p>
<input type="button" onclick="myDecoding()" value="Encode & Decode">
<p id="myId"></p>

<script>
    function myDecoding() {
        var u = "the .NET Inetrview preparation videos?_#$%^&*()";
        var e = encodeURI(U); //* Enocde the string *//
        var d = decodeURI(E); //* Decode th encoded string *//
        var r = "Encode Result is:" + e + "</br></br>" + "Decoding Result is:" + d;
        document.getElementById("myId").innerHTML = r;
    }
</script>

In the above code snippet we have given Id as "myId"to the second <p> element in the HTML code. There is a function myDecoding() in the<script>block which is connected to the onclick of the HTML button and there is a string with value "the .NET Inetrview preparation videos?_#$%^&*()" to the variable u. We need to decode the encoded URI, for that first we need to encode the URI by using encodeURI() function after that we need to decode the encoded URI by using decodeURI() function, variable returns the encodeURI() function result and decodeURI() function result. Onclick of the button "Encode & Decode" in the HTML code fires the function myDecoding() in the <script> block at the same time encodeURI() function encode the URI, decodeURI() function decode the encoded URI and returns the output.

OUTPUT

 

 Views: 4498 | Post Order: 176



Write for us






Hosting Recommendations