cookies
Javascript library for accessing and manipulating HTTP cookies in the web browser.
Get one or a list of cookies, set cookies, delete cookies, test if the browser accepts cookies. When JSON support is available, any JS value can be set to a cookie--it will be automatically serialized before being written, and un-serialzied on read.
installation
bower
bower install jaaulde-cookies
npm
npm install jaaulde-cookies
html
Download the code, link it in your HTML file.
usage
This library is intended for use in the browser to access and manipulate cookies. It provides a singleton API, cookies
.
Cookie options
As you'll see in the docs below, many of the methods can take an options
parameter. The options that can be set are:
Option | Description | Default | Note |
---|---|---|---|
domain | Domain for which the cookie be available | null (current domain) |
|
path | Path for which the cookie be available | '/' |
|
expires | Date object representing expiration date/time of cookie | null (expires when browser closes) |
Setting a past date/time will delete the cookie |
secure | Should cookie be sent to server via HTTPS only? | false |
Test for browser cookie acceptance
cookies.test()
signature
/** * test - test whether the browser is accepting cookies * * @access public * @static * @return */
example
if cookies // browser is accepting cookies!
Set cookies
cookies.set()
signature
/** * set - set or delete a cookie with desired options * * @access public * @static * @param * @param * * @param * @return */
examples
// sets cookie by the name of 'myCookie' to value of 'myValue' with default optionscookies; // sets cookie by the name of 'myCookie' to value of 'myValue' with path of '/somedir'cookies;
Get cookies
cookies.get()
signature
/** * get - get one, several, or all cookies * * @access public * @static * @param * * * @return * * */
examples
// returns value of myCookie if it is present, null if notvar my_cookie = cookies; // returns object in key/value form of each requested cookie if it is present, null if notvar some_cookies = cookies; // returns object in key/value form of all available cookies from your sitevar all_cookies = cookies;
Get filtered list of Cookies
cookies.filter()
signature
/** * filter - get hash of cookies whose names match the provided RegExp * * @access public * @static * @param * @return */
examples
// returns object in key/value form of cookies whose names start with "site"var filtered_cookies = cookies;
Delete Cookies
note: A cookie can only be deleted using the same options with which it was set
cookies.del()
signature
/** * del - delete a cookie (domain and path options must match those with which the cookie was set; this is really an alias for set() with parameters simplified for this use) * * @access public * @static * @param * * @param * @return */
examples
// deletes a cookie, 'myCookie', with default optionscookies; // deletes a cookie by the name of 'myCookie' which had been set with a path of '/somedir'cookies; // deletes all cookiescookies;