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.
MSIE version 3 will only return cookie data for documents that were requested using the http: protocol.
// 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 |
Prev | Home | Next |
Document.contextual() | Up | Document.createAttribute() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |