JavaScript > Global Reference

Encode a string in JavaScript

How to use escape function to encode a string in JavaScript?


The escape() function is used to encode a string in JavaScript.

NOTE: The escape() function is outdated function in the JavaScript, instead of escape() function we are using encodeURI() & encodeURIComponent() in JavaScript.

<p>Click the below button to run the Escape function.</p>
<input type="button" onclick="myFunction()" value="Escape(Encode)">
<p id="myId"></p>

<script>
    function myFunction() {
        var u = "Want to lear technologies? Go through TechFunda.com";
        var r = escape(u);
        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 myFunction() in the<script>block which is connected to the onclick of the HTML button and there is a string with value "Want to lear technologies? Go through TechFunda.com" to the variable uwe need to encode the u variable, for that we are using escape() function in Variable r (Although at present escape() function is not used in JavaScript, for expalining the code we are using escape() function). Onclick of the button "Escape(Encode)" in the HTML code fires the function myFunction() in the <script> block at the same type escape() function encodes the string value and returns as output.

OUTPUT

 Views: 657 | Post Order: 179



Write for us