Cookie API - Usage

cookie.js defines the following functions:

deleteCookie()

Synopsis

deleteCookie(name, path, domain)

Arguments

name

The name of the cookie to be deleted.

path

An optional path for the cookie. This value must match the path used to set the original cookie in order to erase the cookie. If this argument is omitted, a value of '/' will be used.

domain

An optional root domain for the cookie. This value must match the domain used to set the original cookie in order to erase the cookie. If this argument is omitted, the host name of the current server will be used.

Returns

The value previously contained in the deleted cookie. If the cookie couldn't be found, null will be returned.

Description

deleteCookie() immediately deletes the named cookie that matches the path and domain information.

fixDate()

Synopsis

fixDate(date)

Arguments

date

The date to be corrected. This should be a Date object.

Returns

A reference to a Date object that represents the correct date.

Description

fixDate() was intended to work around a bug in some Netscape 2 browsers where all Date objects were incorrect by a day. This function would create and return a correct date for any Date object. It will not break dates on any other browsers. It is included for cases when support of ancient browsers is required.

getCookie()

Synopsis

getCookie(name)

Arguments

name

The name of the cookie to retrieve the value of.

Returns

The value associated with the named cookie. If the cookie doesn't exist, the function returns null.

Description

getCookie() retrieves the value of a previously created cookie.

setCookie()

Synopsis

setCookie(name, value, expires, path, domain, secure)

Arguments

name

The name of the cookie. All cookies from the same domain must have unique names. The name can not contain semi-colons, commas or any whitespace (such as tabs or spaces).

value

The value to be stored with the name.

expires

An optional expiry date. This should be a Date object that specifies when the cookie should be deleted by the system. Dates in the past will erase the cookie. If this argument is omitted, the cookie will last for the rest of the browser session (it will be erased when the browser is closed).

path

An optional path from which the cookie will be visible. This should be a valid path that starts with a '/'. For instance, a path of '/foo' will allow the urls /foo/index.html and /foo/bar/index.html to see the cookie but not /index.html. If this argument is omitted, the cookie will be visible to all pages in the originating domain.

domain

An optional root domain from which the cookie will be visible. For instance, a domain of store.acme.com will allow servers at anvil.store.acme.com and rocket.store.acme.com to see the cookie but not www.acme.com. If this argument is omitted, the host name of the originating domain is used.

secure

An optional flag to indicate if the cookie should be sent over secure connections. If a value of true is used, the cookie will only be sent over secure (https) connections. If a value of false is used, the cookie should be available over both secure and unsecure connections (Netscape, however, will not let such a cookie be visible over secure connections). If this argument is omitted, then the cookie will be unsecure.

Returns

Nothing.

Description

setCookie() associates the value with the name and creates a cookie. If an expiry date is provided, the cookie will become persistent and will be stored by the visitor's computer until that date. Cookies without expiry dates are session cookies and will be erased when the visitor closes their browser. In order to give a new value to an existing cookie, use setCookie() with the same values for the name, path, domain, and secure arguments as was used when creating the original cookie.

supportsCookies()

Synopsis

supportsCookies(rootPath)

Arguments

rootPath

An optional path for the test cookie so that only one will be created if you test on multiple pages with different paths. Use '/' if you control the whole domain. If your site root is www.some-domain.com/~me/, use '/~me/'. If no path is provided, the test cookie will use the path of the HTML document.

Returns

true if cookies are supported, false otherwise.

Description

supportsCookies() determines if a browser supports cookies and has turned on cookie support. It's a good idea to call this function before relying on cookies to store state information.

More about this script

Licensing

This script is released under a Creative Commons License.