DOM:document.cookie
From MDC
Contents |
[edit] Summary
Get and set the cookies associated with the current document.
[edit] Syntax
allCookies = document.cookie;
-
allCookiesis a string containing a semicolon-separated list of cookies (i.e.key=valuepairs)
document.cookie = updatedCookie;
-
updatedCookieis a string of formkey=value. Note that you can only set/update a single cookie at a time using this method.
- Any of the following cookie attribute values can optionally follow the key-value pair, specifying the cookie to set/update, and preceded by a semi-colon separator:
-
;path=path(e.g., '/', '/mydir') -
;domain=domain(e.g., 'example.com', '.example.com' (includes all subdomains), 'subdomain.example.com') -
;max-age=max-age-in-seconds(e.g., 60*60*24*365 for a year) -
;expires=date-in-GMTString-format(could use Date.toGMTString; now obsolete) -
;secure(cookie to only be transmitted over secure protocol as https)
-
- The cookie value string can use encodeURIComponent() to ensure that the string does not contain any commas, semicolons, or whitespace (which are disallowed in cookie values).
[edit] Example
document.cookie = "name=oeschger"; document.cookie = "favorite_food=tripe"; alert(document.cookie); // displays: name=oeschger;favorite_food=tripe
[edit] Notes
Starting with Firefox 2, a better mechanism for client-side storage is available - WHATWG DOM Storage.