SQL PHP HTML ASP JavaScript articles and free scripts to download
JavaScript String

escape & unescape string function

We can use escape function in JavaScript to make a string portable to transmit it over a network. All the non ASCII characters are to be converted before sending them by using escape function. The escape function will replace these non ASCII characters to its two or four digit hex format.

The escape function converts a blank space into %20, same way it converts & to %26

After conversion we can get back the original string by using another JavaScript function unscape

Let us start with some examples

First let us create a string variable and then apply escape function and display it.

var str="this is some text &')";
document.write(str);
document.write("<br>");
document.write(escape(str));

The output of the above code will be

this is some text &')
this%20is%20some%20text%20%26%27%29

As you can see the space in between words and other special chars are replaced by their hex values and now they can be transmitted over net.
Now let us try to get back the original string by using unescape function. We will add one more line to the above code.

var str="this is some text &')";
document.write(str);
document.write("<br>");
document.write(escape(str));
document.write("<br>");
document.write(unescape(str));

The output of this is here ( only the last line ).

this is some text &')

We got our original string.

Be the first to post comment on this article :


Google+

Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked




HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer