Java Reference
In-Depth Information
document.cookie = "hero=true"
<< "hero=true"
document.cookie = "city=Metropolis"
<< "city=Metropolis"
Changing Cookie Values
A cookie's value can be changed by reassigning it to document.cookie using the same
name but a different value. The following code will update the value of both the cookies
that we set in the previous section:
document.cookie = "name=Batman"
<< "name=Batman"
document.cookie = "city=Gotham"
<< "city=Gotham"
Reading Cookies
To see the current contents of the cookie jar, simply enter document.cookie :
document.cookie:
<< "name=Batman; hero=true; city=Gotham"
We can use the split method to break the string into an array containing each name/value
pair, and then use a for loop to iterate through the array:
var cookies = document.cookie.split("; ");
for (var i=0, max=cookies.length; i < max; i++){
var crumbs = cookies[i].split("=");
console.log("The value of " + crumbs[0] + " is " +
crumbs[1]);
}
To see an example of cookies used in the wild, you can visit nearly any website, open the
console, and type document.cookie .
Search WWH ::




Custom Search