js-cookie
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|
JS-Cookie is a simple, lightweight JavaScript API for managing cookies. This robust and easy-to-use tool allows users to create, read, and delete cookies. Moreover, it supports all types of characters and it's thoroughly tested. The API also does not rely on any other JavaScript libraries, meaning it can be used standalone. All these features come in under 800 bytes when gzipped, making JS-Cookie ultra-lightweight, yet highly powerful and practical.
JS-Cookie has a very straightforward usage pattern. Here's how to use it:
To install JS-Cookie, use npm as follows:
npm i js-cookie
You may include it from a CDN like jsDelivr too.
Creating a simple cookie that will be valid across your entire website looks like this:
Cookies.set('name', 'value');
To create a cookie that expires in 7 days and is valid across the entire site:
Cookies.set('name', 'value', { expires: 7 });
If a cookie is to be valid to the path of the current page:
Cookies.set('name', 'value', { expires: 7, path: '' });
Reading cookies is done with:
Cookies.get('name'); // Returns 'value'
Cookies.get('nothing'); // Returns undefined
All visible cookies can be read with:
Cookies.get(); // Returns { name: 'value' }
Removing cookies:
Cookies.remove('name');
More complex usage scenarios include changing the default cookie attributes and encoding or decoding the cookie values. Instructions for these can be found in the JS-Cookie documentation.
The comprehensive documentation for JS-Cookie can be found in the project's GitHub repository README file. It discusses everything from basic usage to advanced topics like using custom encoding/decoding converters and managing potential namespace conflicts. For more specific use cases, there's also a useful wiki linked in the README. Anyone looking to dive into the fully detailed inner workings of the JS-Cookie API can navigate to the latest release documentation from the main branch of the GitHub repository.