Jim's JavaScript Cookie Library

This page demonstrates and documents the JavaScript Cookie Libary, cookies.js, written by Jim Auldridge.
This page and all code within, either directly or by reference, is copyright © Jim Auldridge 2005-2008. All rights reserved.

This component, known as "Jim's JavaScript Cookie Library", has been released as open source under a BSD license and is free for all uses personal, commercial, for-profit, not-for-profit, or any other purpose you can think of.

Version 1.3

Change Log:

How To:

Basics and Name Spacing

After following the instructions below to insert the cookie handling library into your page, four methods will be available to you: get(), set(), del(), and test(). These methods do exactly what you expect they would do: get a cookie, write/overwrite a cookie, delete a cookie, and test if the browser accepts cookies (respectively). The library exists under a namespace I have created in order to help you avoid conflicts in the global namespace. My namespace is jimAuld and within that namespace is a namespace name utils This is where the cookie library resides as cookies. So all of the 4 methods belong to jimAuld.utils.cookies making the methods accessible as: jimAuld.utils.cookies.get()
jimAuld.utils.cookies.set()
jimAuld.utils.cookies.del()
jimAuld.utils.cookies.test()
You could shorten this up by assigning a shortcut to the library:
var c = jimAuld.utils.cookies;
c.get();

However, you should use this carefully as it does somewhat defeat the purpose of name-spacing.

Methods

get()

The get() method takes one parameter, cookieName, which is a string and the name of the cookie whose value is desired. It will return null if the cookie is not set or has an empty value.
var myVar = jimAuld.utils.cookies.get('myCookie');

set()

The set() method takes up to six parameters. The first parameter, cookieName, is a string and names the cookie to be written. The second parameter, value is also a string and is the value to be written to the named cookie. The third parameter, hoursToLive, is the first of the optional parameters and should be a numeric value stating how long the cookie should be good for (used in calculating the expiration date). Passing 0 or leaving this value empty results in the cookie only being good ntil the user closes their browser. A negative value relt in the cookie being deleted, thouh it is preferable to use the del() method for deleting cookies.
...To Be Continued...

Demo:

The purpose of this page is to demonstrate the uses and functionality of the cookie library. Below these paragraphs you will see a set of buttons. By clicking on these buttons, you manipulate a cookie by the name of 'test'. This is all done with the methods of the cookies object that is created in cookies.js.

Suggested flow of testing is as follows:

  1. Click "Test Cookie Acceptance" to test the browsers ability to accept cookies.
  2. Click "Show cookie value" To show the cookie is currently not available.
  3. Click "Set the cookie"
  4. Click "Show cookie value" To show the cookie is now set.
  5. Click "Delete the cookie"
  6. Click "Show cookie value" To show the cookie is, again, not available.

(Step 1)

(Steps 2,4,6)

(Step 3) (Step 5)

Code

Below is the code that builds the object and its methods. The code sample is heavily commented for your learning purposes.

To use this code, place it in the HEAD element of your web page. Or download cookies.js (or the minimal version or the highly commented version) and place it on your server, then reference it from the HEAD element of your page as so:
<script type="text/javascript" src="cookies.js"></script>
(with the src attribute modified to match the version you downloaded)

Contents of cookies.js