browser-cookies
Tiny cookies library for the browser
Features
- Clean and easy to use API
- Small footprint (minified and gzipped ~ 0.5kB)
- No dependencies
- RFC6265 compliant
- Cross browser support
- Supports CommonJS (e.g. Browserify)
Browser compatibility
Cross browser support is verified on real browsers using automated testing:
Or run the unit tests right now in your current browser.
Installation
Using NPM
npm install browser-cookies
Using Bower
bower install browser-cookies
Usage
var cookies = ; cookies;cookies; // Expires after 1 yearcookies; cookies; // Returns cookie value (or null) cookies; // Removes cookie
API
API contents:
- method cookies.set(
name
,value
[,options
]) - method cookies.get(
name
) - method cookies.erase(
name
, [,options
]) - method cookies.all()
- property cookies.defaults
cookies.set(name
, value
[, options
])
Method to save a cookie.
argument | type | description |
---|---|---|
name |
string | The name of the cookie to save. |
value |
string | The value to save, percent encoding will automatically be applied. |
options |
object | May contain any of the properties specified in options below. If an option is not specified, the value configured in cookies.defaults will be used. |
cookies.get(name
)
Method that returns a cookie value, or null if the cookie is not found. Percent encoded values will automatically be decoded.
argument | type | description |
---|---|---|
name |
string | The name of the cookie to retrieve. |
cookies.erase(name
[, options
])
Method to remove a cookie.
argument | type | description |
---|---|---|
name |
string | The name of the cookie to remove. |
options |
object | May contain the domain and path properties specified in options below. If an option is not specified, the value configured in cookies.defaults will be used. |
cookies.all()
Method to get all cookies.
Returns an object containing all cookie values with the cookie names used as keys. Percent encoded names and values will automatically be decoded.
cookies.defaults
This object may be used to change the default value of each option specified in options below.
Options
The options shown in the table below may be set globally using cookies.defaults or passed as function argument to cookies.set() and cookies.erase(). Also check out the Examples further below.
Name | Type | Default | Description |
---|---|---|---|
expires |
Number , Date , String |
0 |
Configure when the cookie expires by using one of the following types as value:
|
domain |
String |
"" |
The domain from where the cookie is readable.
|
path |
String |
"/" |
The path from where the cookie is readable.
|
secure |
Boolean |
false |
If true the cookie will only be transmitted over secure protocols like https. |
httponly |
Boolean |
false |
If true the cookie may only be read by the web server.
|
samesite |
String |
"" |
The samesite argument may be used to prevent cookies from being sent along with cross-site requests.
|
Examples
Count the number of a visits to a page:
var cookies = ; // Get cookie valuevar visits = cookies || 0;console; // Increment the counter and set (or update) the cookiecookies;
JSON may be saved by converting the JSON object into a string:
var cookies = ; // Store JSON datavar user = firstName: 'Sofia' lastName: 'Dueñas';cookies // Retrieve JSON datavar userString = cookies;;
The default cookie options may be changed:
var cookies = ; // Override defaultscookiesdefaultssecure = true;cookiesdefaultsexpires = 7; // 'secure' option enabled and cookie expires in 7 dayscookies // 'secure' option enabled and cookie expires in 30 dayscookies
The cookies.all
method can be used for more advanced functionality, for example to erase all cookies except one:
var cookies = ;var cookieToKeep = 'FirstName'; // Name of the cookie to keep // Get all cookies as an objectvar allCookies = cookiesall; // Iterate over all cookie namesfor var cookieName in allCookies // Erase the cookie (except if it's the cookie that needs to be kept) ifallCookies && cookieName != cookieToKeep cookies;
How to use with PHP
Use setrawcookie() instead of setcookie()
to prevent PHP from replacing spaces with +
characters:
// Set cookiesetrawcookie('fullName', rawurlencode('Lisa Cuddy')); // Get cookie$_COOKIE['fullName'];
Development
The design goal is to provide the smallest possible size (when minified and gzipped) for the given API while remaining compliant to RFC6265 and providing cross-browser compatibility and consistency.
Development setup (requires node and git to be installed):
git clone https://github.com/voltace/browser-cookies.gitcd browser-cookiesnpm install # Install dev dependencies npm run test:local # Run unit tests locally (takes ~5 seconds) npm run build # Create minified version
Feel free to submit an issue on GitHub for any question, bug or feature requesst you may have.
License
Public Domain (UNLICENSE)