CHAPTER 12

image

Cookies

A cookie is a piece of data sent from a web site and stored locally on the user’s computer. They provide a persistent storage space, allowing web sites to remember users as they move between pages or return to the site.

Creating Cookies

Cookies are created by assigning a name-value pair to the document.cookie object. They are limited to storing string values only.

document.cookie = "cookie1=mycookie";

The same object is used for retrieving the cookie string.

console.log(document.cookie); // "cookie1=mycookie"

To modify a cookie, you just need to assign it a new value. This will overwrite the previous cookie.

document.cookie ...

Get JavaScript Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.