Reads and writes a cookie.

  • domain - (string: defaults to false) The domain the cookie belongs to.
  • path - (string: defaults to '/') The path the cookie belongs to.
  • duration - (number: defaults to false) The duration of the cookie (in days) before it expires. If set to false or 0, the cookie will be a session cookie that expires when the browser is closed.
  • secure - (boolean: defaults to false) Stored cookie information can be accessed only from a secure environment.

Writes a cookie in the browser.


var myCookie = Cookie.write(key, value[, options]);


  1. key - (string) The key (or name) of the cookie.
  2. value - (string) The value to set. Cannot contain semicolons.
  3. options - (mixed, optional) See Cookie.


  • (object) An object with the options, the key and the value. You can give it as first parameter to Cookie.dispose.


Saves the cookie for the duration of the session:

var myCookie = Cookie.write('username', 'JackBauer');

Saves the cookie for a day:

var myCookie = Cookie.write('username', 'JackBauer', {duration: 1});


In order to share the cookie with pages located in a different path, the Cookie.options.domain value must be set.

Reads the value of a cookie.


var myCookie =;


  1. name - (string) The name of the cookie to read.


  • (mixed) The cookie string value, or null if not found.


Removes a cookie from the browser.


var oldCookie = Cookie.dispose(name[, options]);


  1. name - (string) The name of the cookie to remove or a previously saved Cookie instance.
  2. options - (object, optional) See Cookie.


Remove a Cookie:

Cookie.dispose('username'); // Bye-bye JackBauer!

Creating a cookie and removing it right away:

var myCookie = Cookie.write('username', 'JackBauer', {domain: ''});
if ('username') == 'JackBauer') { myCookie.dispose(); }


  • Based on the functions by Peter-Paul Koch of QuirksMode.