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.

application of this technology is the identification of individual users. Typically, a site will have a<br />

user log in and will then set a cookie containing the appropriate username. From that point on,<br />

whenever the user makes a request to that particular site, the browser sends the username<br />

cookie in addition to the usual information to the server. <strong>The</strong> server can then keep track of<br />

which user it is serving pages to and modify its behavior accordingly. This is how many Webbased<br />

e-mail systems ―know‖ that you are logged in.<br />

<strong>The</strong>re are several parts to each cookie, many of them optional. <strong>The</strong> syntax for setting cookies is<br />

name=value [; expires=date] [; domain=domain] [; path=path] [; secure]<br />

<strong>The</strong> tokens enclosed in brackets are optional and may appear in any order. <strong>The</strong> semantics of<br />

the tokens are described in Table 16-1.<br />

Table 16-1: <strong>The</strong> Anatomy of a Cookie<br />

Token Description Example<br />

name= Sets the cookie named<br />

name to the string<br />

value.<br />

expires= Sets the expiration date<br />

of the cookie to date.<br />

<strong>The</strong> date string is given<br />

in Internet standard<br />

GMT format. To format<br />

a Date to this<br />

specification you can<br />

use the toGMTString()<br />

method of Date<br />

instances.<br />

domain= Sets the domain for the<br />

cookie to domain,<br />

which must correspond<br />

(with certain flexibility)<br />

to the domain of the<br />

server setting the<br />

cookie. <strong>The</strong> cookie will<br />

be returned only when<br />

making a request of this<br />

domain.<br />

path= String indicating the<br />

subset of paths at the<br />

domain for which the<br />

cookie will be returned.<br />

secure Indicates that the<br />

cookie is only to be<br />

returned over a secure<br />

(HTTPS) connection.<br />

username=<br />

expires=Sun, 01-Dec-2002<br />

08:00:00 GMT<br />

domain=www.javascriptref.com<br />

path=<br />

secure<br />

Cookies that are set without the expires field are called session cookies. <strong>The</strong>y derive their<br />

name from the fact that they are kept for only the current browser session; they are destroyed<br />

when the user quits the browser. Cookies that are not session cookies are called persistent<br />

cookies because the browser keeps them until their expiration date is reached, at which time<br />

they are discarded.<br />

Note Some people refer to session cookies as memory cookies and persistent cookies as disk<br />

cookies.

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

Saved successfully!

Ooh no, something went wrong!