@segma/cookie-operator
v1.2.4
Published
a javascript handler for browser cookie
Downloads
4
Readme
cookie-operator
Used to manipulate cookies for JavaScript
Translations: 中文
Installation
npm install
$ npm i cookie-operator
Direct download
Download the script here and include it (unless you are packaging scripts somehow else):
<script src="/path/to/cookie_operator.min.js"></script>
Or include it via jsDelivr CDN:
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/cookieOperator.mini.js">
Basic Usage
Create a cookie, valid across the entire site:
cookieOperator.set('name', 'value');
Create a cookie that expires 30 days from now, valid across the entire site:
cookieOperator.set('name', 'value',{ expires:30, path:'', domain:document.domain});
Read cookie:
cookieOperator.set('name', 'value');
cookieOperator.set('name2', 'value2');
cookieOperator.get(); // => {name:"value";name2:'value2'}
cookieOperator.get('name'); // => 'value'
cookieOperator.get('otherName'); // => 'undefined'
Delete cookie:
cookieOperator.remove('name');
Delete cookies by keys:
cookieOperator.removeAll(['name1','name2','name3'...]);
Delete a cookie valid to the path of the current page:
cookieOperator.set('name', 'value', { path: '' });
cookieOperator.remove('name'); // fail!
cookieOperator.remove('name', { path: '' }); // removed!
Get the current primary domain name:
// 'www.test.com'
cookieOperator.getTopDomain(); // 'test.com'
// 'name1.name2.test.com'
cookieOperator.getTopDomain(); // 'test.com'
Get the primary domain name based on the given domain name:
cookieOperator.getTopDomain('www.test2.com') // 'test2.com'
IMPORTANT! when deleting a cookie, you must pass the exact same path and domain attributes that was used to set the cookie, unless you're relying on the default attributes.
Create an instance that overrides the default value of the cookie setting :
let testAttr = {
expires: 1,
domain: 'test.com',
path: '/a',
secure: true
};
let _cookieOperator = cookieOperator.create(testAttr)
API
Cookie Attributes
The last parameter of a function cookieOperator.set(...)
is an object, which has several properties that control the cookie.
If you pass in these parameters, the default properties will be overwritten.
For more information on the properties of cookies, please refer to Document.cookie
expires
Define when the cookie will be removed.If the value is a Number
, the deleted date will be the number of days after the creation time; if it is a date object, the deleted time will be the time represented by the date object.
Default: Cookie is removed when the user closes the browser.
Examples:
cookieOperator.set('name', 'value', { expires: 30 });
cookieOperator.get('name'); // => 'value'
cookieOperator.remove('name');
path
This cookie can only be obtained in the path where the cookie is set.
Default: /
Examples:
cookieOperator.set('name', 'value', { path: '' });
cookieOperator.get('name'); // => 'value'
cookieOperator.remove('name', { path: '' });
domain
A String
indicating a valid domain where the cookie should be visible. The cookie will also be visible to all subdomains.
Default: document.domain
Examples:
cookieOperator.set('name', 'value', { domain: 'subdomain.site.com' });
cookieOperator.get('name'); // => undefined (need to read at 'subdomain.site.com')
secure
Either true
or false
, indicating if the cookie transmission requires a secure protocol (https).
Default: false
Examples:
Cookies.set('name', 'value', { secure: true });