Cookie Docs

The cookie library provides a simple API for setting and reading cookies. Using this library in browsers with cookie support you can easily store more pieces of data than you could with the standard browser minimum of support 20 cookies per web server. This is accomplished by storing one or more crumbs of data in a single cookie.

For safety, if you will use FORK.Cookie.isSupported() then keep your number of cookies to 19 or less otherwise keep your cookie use to 20 or less.

The number of crumbs is only limited by the total storage the browser allows for all cookies for a particular web server. The standard minimum is 4 KB but some browsers allow much more.

Cookies are transmitted with each request to the server. Keeping cookie use low will increase request-response speed.

Note that the order you create and store cookies and crumbs is meaningless.

API

isSupported()

Writes a test cookie named FORKtestcookie to the browser and then attempts to read it and then deletes it. If successful this function returns true and if not false. It is a good idea to call this function before using cookies if a particular action must be taken if the browser does not allow cookies.

Examples

if (FORK.Cookie.isSupported()) {
  var c = new FORK.Cookie('myFancyCookie');
  c.foo = 'a value';
  c.store();
} else {
  alert('enable cookies!');
}

Cookie(name)

Create a new cookie object. Each property of this object that does not begin with $ and is not a function will be aggregated and stored in this cookie. These properties are the cookie's crumbs.

name
The string to be used as the name of the cookie. Do not use a name starting with $.

Examples

var c = new FORK.Cookie('myFancyCookie');

store(daysToLive, path, domain, secure)

Store each property of the cookie object that does not begin with $ and is not a function in the one browser cookie. These properties are the cookie's crumbs.

daysToLive
Optional argument. null or a number greater than or equal to zero specifying the number of days until this cookie expires. If this number is zero the cookie will deleted. If this argument is null then the cookie is a session cookie and will be deleted with the browser exits.
path
Optional argument. A string specifying the path of the cookie.
domain
Optional argument. A string specifying the domain of the cookie.
secure
Optional argument. A boolean value. If true the cookie will only be transmitted to the server with https: requests.

Examples

var c = new FORK.Cookie('myFancyCookie');
c.foo = 'a value';
c.bar = 12345;
c.store();
var c = new FORK.Cookie('aCookie');
c.foo = 'a cookie value';
c.store(365, '/');
var c = new FORK.Cookie('myFancyCookie');
c.foo = 'a value';
c.bar = 12345;
c.store();
delete c.foo; // remove the "foo" crumb
c.z = 'xyz'; // add "z" crumb
c.store(); // save the changes

remove(path, domain, secure)

Removes the cookie from the browser. The argument values must match the values used when storing the cookie with store().

path
see store()
domain
see store()
secure
see store()

Examples

var c = new FORK.Cookie('aCookie');
c.foo = 'a cookie value';
c.store(365, '/');
c.remove(365, '/');

Credits

See David Flanagan's book JavaScript: The Definitive Guide for more about cookies. This library's API is based on example 19.2 from David's fifth edition. There ares some browser incompatibilities in the first printing of the fifth edition. The fixes for these problems are in the example 16.1 from the older fourth edition.