Chapter 16. Scripting Cookies

The Document object contains a property named cookie that was not discussed in Chapter 14. On the surface, this property appears to be a simple string value; however, the cookie property controls a very important feature of the web browser and is important enough to warrant a complete chapter of its own.

An Overview of Cookies

A cookie is a small amount of named data stored by the web browser and associated with a particular web page or web site.[52] Cookies serve to give the web browser a memory, so that scripts and server-side programs can use data that was input on one page in another page, or so the browser can recall user preferences or other state variables when the user leaves a page and then returns. Cookies were originally designed for CGI programming, and at the lowest level, they are implemented as an extension to the HTTP protocol. Cookie data is automatically transmitted between the web browser and web server, so CGI scripts on the server can read and write cookie values that are stored on the client. As we’ll see, JavaScript can also manipulate cookies using the cookie property of the Document object.

cookie is a string property that allows you to read, create, modify, and delete the cookie or cookies that apply to the current web page. Although cookie appears at first to be a normal read/write string property, its behavior is actually more complex. When you read the value of cookie, you get a string that contains the names and values ...

Get JavaScript: The Definitive Guide, Fourth Edition 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.