Super Tiny Cookies Management Library - js-cookie

File Size: 8.69 KB
Views Total: 5058
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
Super Tiny Cookies Management Library - js-cookie

js-cookie (previously jQuery Cookie) is a tiny (less than 1kb), cross-browser, and zero-dependency Cookies Management library that makes it easier to read, write and delete cookies in the web app.

You might also like:

How to use it:

1. Install and import the js-cookie with NPM.

# NPM
$ npm i js-cookie --save
import Cookies from 'js-cookie'

2. Or directly import it into the document.

<script type="module" src="/dist/js.cookie.mjs"></script>

<!-- OR -->
<script type="module">
  import Cookies from '/dist/js.cookie.mjs'
</script>

<!-- OR -->
<script nomodule defer src="/dist/js.cookie.js"></script>

3. Set cookies.

// without attributes
Cookies.set('name', 'value');

// with attributes
Cookies.set('name', 'value', {

  // a number of a Date instance
  expires: 365,

  // default: /
  path: '',

  // default: the page where the cookie was created
  domain: 'jqueryscript.net',

  // determines whether cookie transmission requires a secure protocol
  secure: true,

  // sameSite attribute
  sameSite: 'strict',
  
});

4. Read cookies.

// read a cookie
Cookies.get('name');

// read all cookies
Cookies.get();

// read a particular cookie
Cookies.get('name', { 
  domain: 'jqueryscript' 
});

5. Delete cookies.

// remove a cookie
Cookies.remove('name');

// remove a particular cookie
Cookies.remove('name', { 
  path: '' 
});
Cookies.remove('name', { 
  path: '', 
  domain: 'jqueryscript.net' 
})

6. Read & Write cookies with converters.

// read
document.cookie = 'escaped=%u5317'
document.cookie = 'default=%E5%8C%97'
var cookies = Cookies.withConverter({
  read: function (value, name) {
    if (name === 'escaped') {
      return unescape(value)
    }
    // Fall back to default for all other cookies
    return Cookies.converter.read(value, name)
  }
})
cookies.get('escaped') // 北
cookies.get('default') // 北
cookies.get() // { escaped: '北', default: '北' }

// write
Cookies.withConverter({
  write: function (value, name) {
    return value.toUpperCase()
  }
})

Changelog:

v3.0.1 (2021-09-30)

  • Make package.json accessible in export

v3.0.0 (2021-08-01)

  • Migrated to js-cookie.
  • Rewrite doc.

v1.4.1 (2014-04-28)

v1.4.0 (2013-10-06)

  • Support for AMD.
  • Removed deprecated method $.cookie('name', null) for deleting a cookie, use $.removeCookie('name').
  • $.cookie('name') now returns undefined in case such cookie does not exist (was null). Because the return value is still falsy, testing for existence of a cookie like if ( $.cookie('foo') ) keeps working without change.
  • Renamed bower package definition (component.json -> bower.json) for usage with up-to-date bower.
  • Badly encoded cookies no longer throw exception upon reading but do return undefined (similar to how we handle JSON parse errors with json = true).
  • Added conversion function as optional last argument for reading, so that values can be changed to a different representation easily on the fly.

v1.3.1 (2013-03-24)

  • Fixed issue where it was no longer possible to check for an arbitrary cookie, while json is set to true, there was a SyntaxError thrown from JSON.parse.
  • Fixed issue where RFC 2068 decoded cookies were not properly read.

This awesome jQuery plugin is developed by js-cookie. For more Advanced Usages, please check the demo page or visit the official website.