1. Computing & Technology

JavaScript By Example - Cookies

The only way to keep information relating to a site without passing it from page to page to page is to use a cookie. A cookie is also the only way to keep information between browser sessions.

Cookies: 1. Write A Session Cookie

A session cookie keeps data until the browser is closed.

Cookies: 2. Write A First Party Cookie

First party cookies are kept until the expiry date/time is reached.

Cookies: 3. Domain Level Cookies

Specifying a domain allows a cookie to be shared with all the sub-domains of that domain.

Cookies: 4. Folder Specific Cookies

Cookies that are specific to a folder cannot be accessed from outside that folder.

Cookies: 5. Flexible Cookie Writing

We can combine the various cookie writing options together to create a single function that can handle alternatives for writing cookies.

Cookies: 6. Replacing A Cookie

You can replace a cookie simply by writing another cookie with the same name.

Cookies: 7. Deleting A Cookie

The code we use to write cookies can also be used to delete them.

Cookies: 8. Reading Cookies

All the cookies are supplied to the page when it first loads. All we need to do is to extract the individual cookies.

Cookies: 9. Updating Cookies

The difference between replacing and updating a cookie is that in updating a cookie we first need to read the current value stored there.

©2012 About.com. All rights reserved.

A part of The New York Times Company.