How to manage cookies in JavaScript

Jeremy Jeremy (12)
0

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:

javascript
PRIMARY
52 subscribers
code
89 subscribers

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.

document.cookie
What you'll likely see is a long string of semi-colon delineated key/value pairs.
ps=31;_ssd3=P0-1415818163-1436218393727;favorite_animal=goat;_gaa=23.00334.....