There are certain situations in Java Script programming where we can run into a problem storing or passing data. Those situations call for the use of the "escape" functions. For example, when storing data in a browser cookie.
Welcome to Bucaro TecHelp!

Welcome to Bucaro TecHelp!
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

[About BTH]  [User Agreement]  [Privacy Policy]  [Site Map]  [Contact Form]  [Advertise on BTH]  [News Feed]

Google
Web
This Site
   WARNING!
What you learn from these Totally FREE
Einstein Newsletters could cause your friends to mistake you for someone else!
  [] automobiles
  [] business
  [] parenting
  [] computers
  [] contests
  [] education
  [] entertainment
  [] food/wine
  [] free stuff
  [] genealogy
  [] health/fitness
  [] home/garden
  [] humor
  [] marketing
  [] investing
  [] pets
  [] inspiration
  [] self-improve
  [] recreation
  [] travel
  [] womens stuff
  [] writing/reading
Click here and choose as many as you like!

Use Escape to Replace Dangerous Characters

There are certain situations in Java Script programming where we can run into a problem storing or passing data. Those situations call for the use of the "escape" functions. For example, to store data in a browser cookie we can create a cookie with the statement shown below.

document.cookie = "cookieName=cookievalue";

If the cookie string contains the characters ? = , ; : or spaces and tabs, that will cause problems. To store data containing these characters in a cookie, use the escape() function as shown below.

document.cookie = escape("cookieName=cookievalue");

The escape() function will replace all non-alphanumeric characters (except @ * - + . / _) with their hexadecimal equivalents. This is demonstrated with the code shown below.

<html>
<body>
<script language="JavaScript">
document.write("Original characters: " +
  "!#$&'(),:;=?~");
document.write("<br>");
document.write("Escaped characters: " +
  escape("!#$&'(),:;=?~"));
</script>
</body>
</html>

Then to retrieve the cookie, use the unescape() function as shown below to change the hexadecimal character strings back to the original characters.

var cookie_data = unescape(document.cookie);

This is demonstrated with the code shown below.

<html>
<body>
<script language="JavaScript">
document.write("Original characters: " +
  "!#$&'(),:;=?~");
document.write("<br>");
document.write("Escaped characters: " +
  escape("!#$&'(),:;=?~"));
document.write("<br>");
document.write("Unescaped characters: " +
  unescape("%21%23%24%26%27%28%29%2C%3A%3B%3D%3F%7E"));
</script>
</body>
</html>

When an html form is submitted using the GET method, it replaces all dangerous characters before attaching the form's data to the end of the URL. This is demonstrated with the code shown below.

<html>
<body>
<form action="retrieve3.htm">
<input type="text" name="data1" value="Form Data">
<input type="text" name="data2" value="!#$&'(),:;=?~">
<input type="submit" value="Submit">
</form>
</body>
</html>

When you click on forms "Submit" button, the browser will request the webpage named retrieve.htm with the forms data attached to the end of the URL as shown below.

retrieve.htm?data1=Form+Data&data2=%21%23%24%26%27%28
  %29%2C%3A%3B%3D%3F%7E

To retrieve the data, create the webpage retrieve.htm with the code shown below.

<html>
<body>
<script language="JavaScript">
var formdata = document.location.href;
document.write(formdata);
</script>
</body>
</html>

Web Design Sections
Java Script Quick Reference
Java Script Data Types
Java Script Reserved Words
Java Script Arithmetic Operators
Comparison Operators
Java Script Arrays
Java Script Character Encoding and Decoding
The if/else Structure
The switch/case Structure
The for Loop
The while Loop
The break Statement
The continue Statement
JavaScript Math Object
Round a Number
Determine Absolute Value
Determine Minimum and Maximum
Generating Random Numbers
Java Script Trigonometric Methods
Java Script Number Object
Format a Number as Currency
Java Script Strings
Compare Two Strings
Find a Character or a Substring Within a String
Include a Quote Character in a String
Include a Backslash Character in a String
Define Lines in a String
Use Escape to Replace Dangerous Characters
Convert a Number to a String
Convert a String to a Number
The Document Object Model (DOM)
Accessing Web Page Elements
Interactively Set Webpage Colors
Get Webpage File Date and File Size
Dueling Windows
Cookie Power Made Easy

[Site User Agreement]  [Advertise on This site]  [Search This Site]  [Contact Form]
Copyright©2001-2007 Bucaro TecHelp P.O.Box 18952 Fountain Hills, AZ 85269