a javascript handler for browser cookie

Usage no npm install needed!

<script type="module">
  import segmaCookieOperator from 'https://cdn.skypack.dev/@segma/cookie-operator';



Build Status codecov npm build

Used to manipulate cookies for JavaScript

Translations: 中文


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/cookie-operator@1.2.1/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:


Delete cookies by keys:


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 document

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.


cookieOperator.set('name', 'value', { expires: 30 });
cookieOperator.get('name'); // => 'value'


This cookie can only be obtained in the path where the cookie is set.

Default: /


cookieOperator.set('name', 'value', { path: '' });
cookieOperator.get('name'); // => 'value'
cookieOperator.remove('name', { path: '' });


A String indicating a valid domain where the cookie should be visible. The cookie will also be visible to all subdomains.

Default: document.domain


cookieOperator.set('name', 'value', { domain: 'subdomain.site.com' });
cookieOperator.get('name'); // => undefined (need to read at 'subdomain.site.com')


Either true or false, indicating if the cookie transmission requires a secure protocol (https).

Default: false


Cookies.set('name', 'value', { secure: true });