Gathering detailed insights and metrics for browser-cookies
Gathering detailed insights and metrics for browser-cookies
Gathering detailed insights and metrics for browser-cookies
Gathering detailed insights and metrics for browser-cookies
npm install browser-cookies
Typescript
Module System
Node Version
NPM Version
Added support for SameSite and added TypeScript definitions
Updated on Dec 28, 2017
Extended API with a method to retrieve all cookies, also fixed performance issue
Updated on Jan 15, 2017
Allow expire date to be set to unix epoch
Updated on Apr 25, 2015
Encode cookie names as RFC2616 token
Updated on Apr 17, 2015
Another documentation update
Updated on Apr 16, 2015
Documentation update
Updated on Apr 16, 2015
JavaScript (97.06%)
HTML (2.94%)
Total Downloads
27,443,510
Last Day
17,112
Last Week
100,904
Last Month
418,185
Last Year
4,354,825
Unlicense License
89 Stars
157 Commits
19 Forks
1 Watchers
2 Branches
6 Contributors
Updated on Aug 12, 2024
Latest Version
1.2.0
Package Id
browser-cookies@1.2.0
Size
6.26 kB
NPM Version
3.10.9
Node Version
6.9.2
Published on
Dec 28, 2017
Cumulative downloads
Total Downloads
Last Day
57.5%
17,112
Compared to previous day
Last Week
35.9%
100,904
Compared to previous week
Last Month
5.3%
418,185
Compared to previous month
Last Year
13.6%
4,354,825
Compared to previous year
Tiny cookies library for the browser
Cross browser support is verified on real browsers using automated testing:
Or run the unit tests right now in your current browser.
Using NPM
npm install browser-cookies
Using Bower
bower install browser-cookies
1var cookies = require('browser-cookies'); 2 3cookies.set('firstName', 'Lisa'); 4cookies.set('firstName', 'Lisa', {expires: 365}); // Expires after 1 year 5cookies.set('firstName', 'Lisa', {secure: true, domain: 'www.example.org'}); 6 7cookies.get('firstName'); // Returns cookie value (or null) 8 9cookies.erase('firstName'); // Removes cookie
API contents:
name
, value
[, options
])name
)name
, [, options
])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.
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.
|
Count the number of a visits to a page:
1var cookies = require('browser-cookies'); 2 3// Get cookie value 4var visits = cookies.get('count') || 0; 5console.log("You've been here " + parseInt(visits) + " times before!"); 6 7// Increment the counter and set (or update) the cookie 8cookies.set('count', parseInt(visits) + 1, {expires: 365});
JSON may be saved by converting the JSON object into a string:
1var cookies = require('browser-cookies'); 2 3// Store JSON data 4var user = {firstName: 'Sofia', lastName: 'Dueñas'}; 5cookies.set('user', JSON.stringify(user)) 6 7// Retrieve JSON data 8var userString = cookies.get('user'); 9alert('Hi ' + JSON.parse(userString).firstName);
The default cookie options may be changed:
1var cookies = require('browser-cookies'); 2 3// Override defaults 4cookies.defaults.secure = true; 5cookies.defaults.expires = 7; 6 7// 'secure' option enabled and cookie expires in 7 days 8cookies.set('FirstName', 'John') 9 10// 'secure' option enabled and cookie expires in 30 days 11cookies.set('LastName', 'Smith', {expires: 30})
The cookies.all
method can be used for more advanced functionality, for example to erase all cookies except one:
1var cookies = require('browser-cookies'); 2var cookieToKeep = 'FirstName'; // Name of the cookie to keep 3 4// Get all cookies as an object 5var allCookies = cookies.all(); 6 7// Iterate over all cookie names 8for (var cookieName in allCookies) { 9 // Erase the cookie (except if it's the cookie that needs to be kept) 10 if(allCookies.hasOwnProperty(cookieName) && cookieName != cookieToKeep) { 11 cookies.erase(cookieName); 12 } 13}
Use setrawcookie() instead of setcookie()
to prevent PHP from replacing spaces with +
characters:
1// Set cookie 2setrawcookie('fullName', rawurlencode('Lisa Cuddy')); 3 4// Get cookie 5$_COOKIE['fullName'];
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):
1git clone https://github.com/voltace/browser-cookies.git 2cd browser-cookies 3npm install # Install dev dependencies 4npm run test:local # Run unit tests locally (takes ~5 seconds) 5npm run build # Create minified version
Feel free to submit an issue on GitHub for any question, bug or feature requesst you may have.
Public Domain (UNLICENSE)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
branch protection is not maximal on development and all release branches
Details
Reason
Found 8/29 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More