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 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: function ()
example
if (cookies.test()) {
}
Set cookies
cookies.set()
signature
set: function (n, v, o)
examples
cookies.set('myCookie', 'myValue');
cookies.set('myCookie', 'myValue', {path: '/somedir'});
Get cookies
cookies.get()
signature
get: function (n)
examples
var my_cookie = cookies.get('myCookie');
var some_cookies = cookies.get(['myCookie', 'myOtherCookie']);
var all_cookies = cookies.get();
Get filtered list of Cookies
cookies.filter()
signature
filter: function (p)
examples
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: function (n, o)
examples
cookies.del('myCookie');
cookies.del('myCookie', {path: '/somedir'});
cookies.del(true);