Section 19.2. Storing Cookies


19.2. Storing Cookies

To associate a transient cookie value with the current document, simply set the cookie property to a string of the form:

 name=value 

For example:

 document.cookie = "version=" + encodeURIComponent(document.lastModified); 

The next time you read the cookie property, the name/value pair you stored is included in the list of cookies for the document. Cookie values may not include semicolons, commas, or whitespace. For this reason, you may want to use the core JavaScript global function encodeURIComponent( ) to encode the value before storing it in the cookie. If you do this, you'll have to use the corresponding decodeURIComponent( ) function when you read the cookie value. (It is also common to see code that uses the older escape( ) and unescape( ) functions, but these are now deprecated.)

A cookie written with a simple name/value pair lasts for the current web-browsing session but is lost when the user exits the browser. To create a cookie that can last across browser sessions, specify its lifetime (in seconds) with a max-age attribute. You can do this by setting the cookie property to a string of the form:

 name=value; max-age=seconds 

For example, to create a cookie that persists for a year, you can use code like this:

 document.cookie = "version=" + document.lastModified +                   "; max-age=" + (60*60*24*365); 

You can also specify the lifetime of a cookie with the obsolete expires attribute, which should be set to a date in the format written by Date.toGMTString( ). For example:

 var nextyear = new Date( ); nextyear.setFullYear(nextyear.getFullYear( ) + 1); document.cookie = "version=" + document.lastModified +                   "; expires=" + nextyear.toGMTString( ); 

Similarly, you can set the path, domain, and secure attributes of a cookie by appending strings of the following format to the cookie value before that value is written to the cookie property:

 ; path=path ; domain=domain ; secure 

To change the value of a cookie, set its value again using the same name, path, and domain along with the new value. You can change the lifetime of a cookie when you change its value by specifying a new max-age or expires attribute.

To delete a cookie, set it again using the same name, path, and domain, specifying an arbitrary (or empty) value, and a max-age attribute of 0 (or use the expires attribute to specify an expiration date that has already passed). Note that the browser is not required to delete expired cookies immediately, so a cookie may remain in the browser's cookie file past its expiration date.

19.2.1. Cookie Limitations

Cookies are intended for infrequent storage of small amounts of data. They are not intended as a general-purpose communication or data-transfer mechanism, so you should use them in moderation. RFC 2965 encourages browser manufacturers to allow unlimited numbers of cookies of unrestricted size. You should know, however, that the standard does not require browsers to retain more than 300 cookies total, 20 cookies per web server (for the entire server, not just for your page or site on the server), or 4 KB of data per cookie (both name and value count toward this 4 KB limit). In practice, modern browsers allow many more than 300 cookies total, but the 4 KB size limit is still enforced by some.




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net