11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

When a user connects to a site, the browser checks its list of cookies for a match. A match is<br />

determined by examination of the URL of the current request. If the domain and path in a<br />

cookie match the given URL (in some loose sense), the cookie‘s name= token is sent to the<br />

server. If multiple cookies match, the browser includes each match in a semicolon-separated<br />

string. For example, it might return<br />

username=fritz; favoritecolor=green; prefersmenus=<br />

Be aware that we are glossing over some subtleties with regard to how the browser determines<br />

a match. Full details are found at http://home.netscape.com/newsref/std/cookie_spec.html.<br />

Several RFCs (2109, 2965, and especially 2964) also have bearing on cookie technology, but<br />

the Netscape specification is the one widely used.<br />

Cookies in <strong>JavaScript</strong><br />

One nice thing about cookies is that nearly every browser in existence with <strong>JavaScript</strong> support<br />

also provides scripts access to cookies. Cookies are exposed as the cookie property of the<br />

Document object. This property is both readable and writeable.<br />

Setting Cookies<br />

When you assign a string to document.cookie, the browser parses it as a cookie and adds it to<br />

its list of cookies. For example,<br />

document.cookie = "username=fritz; expires=Sun, 01-Dec-2005 08:00:00<br />

GMT;<br />

path=<br />

sets a persistent cookie named username with value ―fritz‖ that expires in 2005 and will be sent<br />

whenever a request is made for a file under the ―/home‖ directory on the current Web server.<br />

Whenever you omit the optional cookie fields (like secure or domain), the browser fills them in<br />

automatically with reasonable defaults—for example, the domain of the current URL and path<br />

to the current document. It is possible, but not recommended, to set multiple cookies of the<br />

same name with differing paths. If you do so, then both values may be returned in the cookie<br />

string, and if so you have to check to see if you can tell the difference using their order in the<br />

string. Attempting to set cookies for inappropriate domains or paths (for example, domain<br />

names other than domains closely related to the current URL) will silently fail.<br />

<strong>The</strong> cookie parsing routines used by the browser assume that any cookies you set are well<br />

formed. <strong>The</strong> name/value pair must not contain any whitespace characters, commas, or<br />

semicolons. Using such characters can cause the cookie to be truncated or even discarded. It<br />

is common practice to encode cookie values that might be problematic before setting them in<br />

the cookie. <strong>The</strong> global escape() and unescape() methods available in all major browsers are<br />

usually sufficient for the job. <strong>The</strong>se functions URL-encode and URL-decode the strings that are<br />

passed to them as arguments and return the result. Problematic characters such as<br />

whitespace, commas, and semicolons are replaced with their equivalent in URL escape codes.<br />

For example, a space character is encoded as %20. <strong>The</strong> following code illustrates their use:<br />

var problemString = "Get rid of , ; and ?";<br />

var encodedString = escape(problemString);<br />

alert("Encoded: " + encodedString + "\n" + "Decoded: " +<br />

unescape(encodedString));

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!