Thursday, April 17, 2008

How do I access the value of a cookie using JavaScript?

How do I access the value of a cookie using JavaScript?

You can manipulate cookies in JavaScript with the document.cookie property. You can set a cookie by assigning this property, and retrieve one by reading its current value.

The following statement, for example, sets a new cookie with a minimum number of attributes:

document.cookie = "cookieName=cookieValue";

And the following statement displays the property's value:

alert(document.cookie);

The value of document.cookie is a string containing a list of all cookies that are associated
with a web page. It consists, that is, of name=value pairs for each cookie that matches the
current domain, path, and date. The value of the document.cookie property, for instance,
might be the following string:

cookieName1=cookieValue1; cookieName2=cookieValue2;

No comments:

Post a Comment