Document.cookie (Property)

Access to a cookie for the current document.

Availability:

DOM level - 1
JavaScript - 1.0
JScript - 1.0
Internet Explorer - 3.02
Netscape - 2.0
Opera - 3.0
Property/method value type:String primitive
JavaScript syntax:-myDocument.cookie

A cookie is a small fragment of textual data that is associated with the current page. It is not modelled particularly well for access by JavaScript and you will need to use some scripting to disassemble and reassemble the name-value pairs that are concatenated together to make the cookie.

The cookie property for a document returns a string containing ALL the cookies that apply to the document. You will need to split them into individual cookies by separating them at semi-colon boundaries. From that you will for each cookie obtain a name=value construct that you can further dismantle and process.

Note that you will not get any of the special attributes of the cookie since they are write-only. The only thing you can get back is its value property. This means that although the cookie property is available for read and write access, unlike all other properties it is not symmetrical. You don't get back out what you put in.

Warnings:

Example code:

   // Define a cookie for the current document

   document.cookie = "cookiename=value";

   // Define a cookie from a variable using URL escape()

   document.cookie = "cookiename=" + escape(myValue);

   // Setting an expiry date on the cookie

   myCookieValue   = "cookiename=value";

   myCookieExpires = "expires="+myDate.toGMTString();

   myCookie = myCookieValue + "; " + myCookieExpires;

See also:Cookie, Document object