Cookies are small items of data, each consisting of a name and a value, stored on behalf of a website by visitors’ web browsers.
In JavaScript, cookies can be accessed through the document.cookie
object, but the interface provided by this object is very primitive.
Cookies.js is a JavaScript object that allows cookies to be created, retrieved, and deleted through a simple and intuitive interface.
Download Cookies.js
Download one of the files below and either incorporate it into your code or serve it as a separate file.
File | Size | Description |
---|---|---|
Cookies.js | 661 bytes | Minified version |
Cookies.src.js | 3,957 bytes | Full version, with comments |
The code creates a global object, Cookies
, which has functions to create, retrieve, and delete cookies.
Creating cookies
Cookies can be created using the set
function, which takes the cookie name and value as parameters:
1 2 |
|
A cookie set in this way will be deleted when the visitor closes their web browser, will only be accessible within the current directory on the current domain name, and will be sent over both encrypted and unencrypted connections. These features can be controlled through an optional third parameter, which is an object with four possible properties.
The expiry
property allows the cookie can be given an expiry date.
Cookies with an expiry date will persist between browsing sessions, and only be deleted when the expiry date is reached or the visitor instructs the browser to do so.
The value of the property can be either a date on which the cookie will expire or a number of seconds after which the cookie should expire:
1 2 3 4 5 |
|
Every cookie is accessible only within a particular path, which defaults to the current directory — for example, a cookie set on a page at http://example.com/news/ would by default be accessible from http://example.com/news/archives/ but not from the home page of the site.
The path
property allows an alternative path to be specified:
1 2 3 4 5 |
|
Every cookie is accessible only on a particular domain, which defaults to the current domain and its subdomains — for example, a cookie set on news.example.com would by default be accessible from archives.news.example.com but not from the main domain example.com.
The domain
property allows an alternative domain to be specified:
1 2 |
|
Finally, the secure
property can be set to true to instruct the browser to send the cookie only over encrypted connections:
1 2 |
|
Retrieving cookies
The value of a cookie can be retrieved using the get
function, which takes the cookie name as a parameter:
1 2 |
|
If there is no cookie with the specified name, the value undefined
is returned.
There may be more than one cookie with the same name if they were set for different paths or subdomains.
In this case the get function returns the most specific cookie (the one set for the longest path).
Passing true
as a second parameter to the get
function will cause it to return an array containing the values of all cookies with the specified name, in order of specificity:
1 2 |
|
Deleting cookies
A cookie can be deleted using the clear
function, which takes the cookie name as a parameter:
1 2 |
|
If the cookie was set for a path or domain other than the current path and domain, these must be passed to the clear
function through its optional second parameter:
1 2 3 4 5 6 7 |
|