dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Jun 1, 2021

Javascript Escape and Unescape Functions

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!

MS Sridhar
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date