JavaScript > Global Reference

Decoding URI component in JavaScript

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


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

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

<script>
    function myDecoding() {
        var u = "http://www.dotnetfunda.com/advices/show/1125/9-years-stagnant-career-and-looking-of-job-in-dot-net-platform?_#$%^&*()";
        var e = encodeURIComponent(U); //* Encode the String *//
        var d = decodeURIComponent(E); //* Decode the Encoded String *//
        var r = "Encoded URI component Result is:" + e + "</br></br>" + "Decoded URI component 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 "http://www.dotnetfunda.com/advices/show/1125/9-years-stagnant-career-and-looking-of-job-in-dot-net-platform?_#$%^&*()to the variable U. We need to decode the encoded URI component, for that first we need to encode the URI component by using encodeURIComponent() function after that we need to decode the encoded URI component by using decodeURIComponent() function, variable returns the encodeURIComponent() function result and decodeURIComponent() function result. Onclick of the button "Encode&Decode" in the HTML code fires the function myDecoding() in the <script> block at the same timeencodeURIComponent() function encode the URI component, decodeURIComponent() function decode the encoded URI component and returns the output.

OUTPUT

 Views: 5372 | Post Order: 178



Write for us






Hosting Recommendations