How to manage cookies in JavaScript

Jeremy Jeremy (12)

At some point you'll want to read, set, and remove cookies using JavaScript. This is actually way more complicated than it should be so I'll provide a few example functions and refer you to a solid jQuery plugin.

Posted in these interests:

27 guides
65 guides

First you should understand what the cookie is and how it's set. A cookie is a string that is stored on the user's computer to allow data to persist throughout the user's session. Cookies are often set by HTTP response headers, but they can also be set directly in the browser using JavaScript. To read the entire cookie string you can access document.cookie.

What you'll likely see is a long string of semi-colon delineated key/value pairs.