Cookie Manager

  • Fri 30 SepSat 29 Oct
  • 18:1318:32
  • Comments

Updated on Sat 29 Oct: Removed dependency on Prototype's $ function.

A simple object to encapsulate cookie handling.

Download:

Features

  • Get, set and clear cookies.
  • Also provides a means of using Internet Explorer's proprietary userData behaviour for larger, persistent storage.

Usage

Initialisation of a CookieManager object:

var manager = new CookieManager();
var manager = new CookieManager({shelfLife:30});

Using IE's userData behaviour instead of cookies in IE:

var manager = new CookieManager({userData:true});

Using the cookie manager:

manager.setCookie("myCookie", aVariable);
var cookieValue = manager.getCookie("myCookie");
manager.clearCookie("myCookie");

Dependencies

None.

Reference

Method Kind Argument Description
CookieManager([options]) constructor
options
options for this instance of a CookieManager
Creates an instance of this object using the given options, if any
getCookie(cookieName) instance
cookieName
the name of a cookie
Returns the value of the specified cookie if it exists, null otherwise
setCookie(cookieName, value) instance
cookieName
the name of a cookie
value
a string to be stord in a cookie
Stores the given value in a cookie with the given name
clearCookie(cookieName) instance
cookieName
the name of a cookie
Clears the cookie with the given name

The options argument object

Property Type Default Description
shelfLife Number 365 How long should the cookie expire after
userData Boolean false When running on Internet Explorer, should its proprietary userData behaviour be used for storage instead of cookies