dcsimg
 

Javascript Escape and Unescape Functions

Tuesday Jun 1st 2021 by MS Sridhar
Article Image

This quick tip looks at two deprecated JavaScript functions - escape() and unescape() - web developers could use to encode and decode strings.

JavaScript had a special function developers could use to encode - and decode - strings. When you encoded a string, it made the string portable, meaning that it could be transmitted over any existing network to any connected computer that supported ASCII characters. Specifically, escape() was used to encode special characters. JavaScript’s escape() function can encode any special character, except for: *, @, -, _, +, ., and /.

Note: : JavaScript’s escape() function has been deprecated as of JavaScript version 1.5. It was replaced with encodeURI() and encodeURIComponent(), so be sure to use those functions instead.

On the flipside, JavaScript’s unescape() function was used to decode an encoded string.

Note: : JavaScript’s unescape() function has been deprecated as of JavaScript version 1.5. It was replaced with encodeURI() and encodeURIComponent(), so be sure to use those functions instead.

Below you can find an example of encoding and decoding encoded strings using JavaScript’s escape() and unescape() functions:

<!DOCTYPE html>

<html>

<body>

 <script>

 var escapeUnescapeStr = escape("Happy Birthday !");

document.write(escapeUnescapeStr);

document.write("<BR>");

 

escapeUnescapeStr = unescape(escapeUnescapeStr);

document.write(escapeUnescapeStr);

</script>

 </body>

</html>

 

The following output would occur if you had used the escape() and unescape functions:

Happy%20Birthday%20%21
Happy Birthday !

We will detail how to use the decodeURI() and decodURIComponent() functions in a later article. Stay tuned!

Home
Mobile Site | Full Site