Cookies: Read, write and delete cookie values

Recommend this page to a friend!
  Info   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 120 All time: 387 This week: 5Up
Version License JavaScript version Categories
cookie-object 1Free for non-comm...1.0Browser
Description Author

This object can read, write and delete cookie values.

It can set the values of cookies optionally setting the expiry time and the path the cookies apply.

The object can also retrieve and delete previously set cookie values.

Picture of Stephen Chapman
  Performance   Level  
Name: Stephen Chapman <contact>
Classes: 11 packages by
Country: Australia Australia
Age: ???
All time rank: 171 in Australia Australia
Week rank: 6 Up1 in Australia Australia Equal
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

We can make all of our cookie processing much simpler if we create our own cookie object with read, write and delete methods that work closer to the way you'd expect these to work than directly referencing the document.cookie object. As well as being able to write session or first party cookies (retained between browser sessions) we can also restrict the cookie to a specific sub-domain of folder. The write method caters for this by allowing between two and four parameters. The first parameter is the cookie name, the second is the value that cookie is to have. If a third parameter is specified then it is the number of days that the cookie will be retained, if this parameter is omitted then a session cookie will be created. The optional fourth parameter if it starts with a / identifies the folder that the cookie is restricted to, otherwise it identifies the domain the cookie is restricted to. To ensure that cookies written by a page can be read back by the same page without the page being reloaded (something not normally possible with cookies) the cookie is also saved as a property of the cookie object. With this particular cookie object the read method simply specifies the name of the cookie you want to read and if it exists it returns the value of that cookie and if it doesn't exist it returns null. The read method first checks if the cookie has been saved as a property of the cookie object before looking at the cookies read in by the page in order to pick up the latest value for the cookie it the cookie has already been (re)written by this page. The delete method simply calls the write method with a date in the past and then deletes the associated property from the cookie object.
  Files folder image Files  
File Role Description
Plain text file cookie.js Class The cookie wrapper object
Accessible without login Plain text file examples.js Example examples of each of the possible calls
Accessible without login Plain text file readme Doc. Description of how each method works

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:387
This week:5Up