Small client-side javascript library that makes managing cookies easy.
Features
Browser Compatibility
Install
API Reference
- RFC6265 compliant
- Cross browser
- Lightweight
- No dependencies
The following browsers have passed all of the automated Cookies.js tests:
- Chrome
- Firefox 3+
- Safari 4+
- Opera 10+
- Internet Explorer 6+
v1.1.4 (~ 13.94 KB) v1.1.4 Minified (~ 2.52 KB)
npm i advanced-cookie-manager
yarn add advanced-cookie-manager
Methods
acm.initialize(options)
acm.set(key, value, options = {})
acm.get(key)
acm.unset(key)
A number (of seconds), a number parsable string, or a Date
object of when the cookie will expire. By default is 0 (session cookie).
Example Usage
acm.expires = 3600; // Expires number format 1 hour
acm.expires = '3600'; // Expires string format 1 hour
acm.expires = new Date(2020, 0, 1); // Expires at Wed Jan 01 2020 00:00:00 GMT+0200
A string value of the path of the cookie. By default is '/'.
Example Usage
acm.path = '/'; // Path for all pages
acm.path = '/cart'; // Path only for /cart page
acm.path = '/success'; // Path only for /success page
A string value of the domain of the cookie. By default is equal to current domain.
Example Usage
acm.domain = 'www.example.com'; // Set www.example.com as default domain
A boolean value of whether or not the cookie should only be available over SSL. By default is false.
Just now it deprecated and dosen't use.
Enable debug option set show in console information about create/delee cookie.
Example Usage
acm.debug = false; // disable
acm.debug = true; // enable
Set encode cookie encodeUri() value (encode by default), you can disable/enable this option.
Example Usage
acm.encode = false; // disable
acm.encode = true; // enable
Set default options for all new cookies
Example Usage
// Initialize all options
acm.initialize({
expires : 3600,
path : '/',
domain : 'www.example.com',
});
// Initialize expires
acm.initialize({
expires : 3600,
});
And now all new cookies without options has this options as default.
Sets a cookie in the document. If the cookie already exist, it will be rewrite it.
Option | Description | Default |
---|---|---|
expires | A number (of seconds), a number parsable string, or a Date object of when the cookie will expire |
0 |
path | A string value of the path of the cookie | / |
domain | A string value of the domain of the cookie | empty |
secure | A boolean value of whether or not the cookie should only be available over SSL (deprecated) | false |
Example Usage
// Setting a cookie value
acm.set('key', 'value');
// Setting cookies with additional options
acm.set('key', 'value', { domain: 'www.example.com'});
// Setting cookies with expiration values
acm.set('key', 'value', { expires: 3600 }); // Expires in 1 hour
acm.set('key', 'value', { expires: '3600' }); // Expires in 1 hour
acm.set('key', 'value', { expires: new Date(2020, 0, 1) }); // Expires at Wed Jan 01 2020 00:00:00 GMT+0200
Returns the value of the most locally scoped cookie with the specified key.
Example Usage
// Get the cookie value
acm.get('key'); // "value"
If key
is empty acm()
method return all locally scoped cookies as array of items (cookies).
Example Usage
// Get all cookies
acm.get(); // [[name : name, value : value], [...], ...}]
Unse the most locally scoped cookie with the specified key.
Example Usage
// Unset the cookie
acm.unset('key');