Small client-side javascript library that makes managing cookies easy.
Features
Browser Compatibility
Getting the Library
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.0.2 Minified (~ 2.47 KB)
v1.0.2 Unminified (~ 10.4 KB)
Properties
Expires
Path
Domain
Secure
Additional properties
Debug
Encode
Methods
acm.initialize(options)
[acm.resetOptions()](#reset 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+0200A 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 pageA 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 domainA 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; // enableSet encode cookie encodeUri() value (encode by default), you can disable/enable this option.
Example Usage
acm.encode = false; // disable
acm.encode = true; // enableSet 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 withot options has this options as default.
Also can reset all options to default with acm.resetOptions method.
Unset all setted cookies options to deafault.
Example Usage
// Reset options to deafult
acm.resetOptions();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 |
Also can reset all options to default with acm.resetOptions method.
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
// Using the alias
acm('key', 'value', { secure: true });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 JSON object.
Example Usage
// Get all cookies
acm.get(); // cookies [{id : id, name : name, value : value},{...}]Unse the most locally scoped cookie with the specified key.
Example Usage
// Unset the cookie
acm.unset('key');