Skip to content

JAAulde/cookies

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

jQuery Plugin: The jQuery plugin which was once distributed directly with this library has been moved to its own project (which depends on this one).

GitHub version Bower version NPM version

installation

bower install jaaulde-cookies
npm install jaaulde-cookies

html

Download the code, link it in your HTML file.

<script src="/path/to/jaaulde-cookies.js"></script>

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 {boolean}
 */
test: function ()
example
if (cookies.test()) {
    // browser is accepting cookies!
}

Set cookies

cookies.set()

signature
/**
 * set - set or delete a cookie with desired options
 *
 * @access public
 * @static
 * @param {string} n - name of cookie to set
 * @param {mixed} v - Any JS value. If not a string and JSON support present will be JSON encoded
 *                  {null} to delete
 * @param {object} o - optional list of cookie options to specify
 * @return {void}
 */
set: function (n, v, o)
examples
// sets cookie by the name of 'myCookie' to value of 'myValue' with default options
cookies.set('myCookie', 'myValue');

// sets cookie by the name of 'myCookie' to value of 'myValue' with path of '/somedir'
cookies.set('myCookie', 'myValue', {path: '/somedir'});

Get cookies

cookies.get()

signature
/**
 * get - get one, several, or all cookies
 *
 * @access public
 * @static
 * @param {mixed} n {string} name of single cookie
 *                  {array} list of multiple cookie names
 *                  {void} if you want all cookies
 * @return {mixed} type/value of cookie as set
 *                 {null} if only one cookie is requested and is not found
 *                 {object} hash of multiple or all cookies (if multiple or all requested)
 */
get: function (n)
examples
// returns value of myCookie if it is present, null if not
var my_cookie = cookies.get('myCookie');

// returns object in key/value form of each requested cookie if it is present, null if not
var some_cookies = cookies.get(['myCookie', 'myOtherCookie']);

// returns object in key/value form of all available cookies from your site
var all_cookies = cookies.get();

Get filtered list of Cookies

cookies.filter()

signature
/**
 * filter - get hash of cookies whose names match the provided RegExp
 *
 * @access public
 * @static
 * @param {RegExp} p The regular expression pattern to match against cookie names
 * @return {object} hash of cookies whose names match the RegExp
 */
filter: function (p)
examples
// returns object in key/value form of cookies whose names start with "site"
var filtered_cookies = cookies.filter(/^site/);

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 {mixed} n {string} name of cookie to delete
 *                  {boolean} true to delete all
 * @param {object} o optional list of cookie options to specify (path, domain)
 * @return {void}
 */
del: function (n, o)
examples
// deletes a cookie, 'myCookie', with default options
cookies.del('myCookie');

// deletes a cookie by the name of 'myCookie' which had been set with a path of '/somedir'
cookies.del('myCookie', {path: '/somedir'});

// deletes all cookies
cookies.del(true);

About

Javascript library for accessing and manipulating HTTP cookies in the web browser.

Resources

License

Stars

Watchers

Forks

Packages

No packages published