1.1.0 • Published 2 years ago
cookie-master.js v1.1.0
Cookie Master.js
A simple and concise library for working with Cookies in JavaScript. Now with JSON's supporting!
Why us?
- Simple & lightweight (Minified version size - 602 B)
- Works with JSON from the box (returns JSON object if necessary, sets JSON cookie automatically)
- You can specify any parameter while setting cookie
- Works with all browsers (Including ES6!)
- Written on pure JS (no dependencies)
- Has minified version
- Can be installed by npm or used by unpkg CDN
Installations
1. NPM package manager
Through the terminal, run the following command in the root folder of the project:
npm i cookielib
In .js files, use import with the desired module path:
import {Cookie} from './node_modules/cookie-master.js/src/cookie-master.js';
Don't forget that the .js file must be included as a module:
<script src="index.js" type="module"></script>
2. Minified version (unpkg CDN)
<!-- Main version -->
<script src="https://unpkg.com/cookielib/src/cookie.js"></script>
<!-- Minified version -->
<script src="https://unpkg.com/cookielib/src/cookie.min.js"></script>
3. Build from source
Go to /src
folder of GitHub page and download main or minified version of script. Then, include script to your HTML page using
<script src="../path/to/your/scripts/folder/cookie.min.js"></script>
Usage
Create a session Cookie:
Cookie.set('key', 'value'); // string Cookie
Cookie.set('user', {name: 'Alex', age: 18}); // JSON Cookie
Create a Cookie with options:
// Expiring Cookie
Cookie.set('name', 'value', {expires: new Date('2020-10-10 03:00:00')});
//
Cookie.set('name', {key: 'value'}, {expires: new Date('2020-10-10 03:00:00')});
Don't forget that JSON format supports only double quotes.
With setCookie()
function, you can specify any cookie options you want. You can find options table there.
Get a cookie:
getCookie('name') // string cookies
getCookie('name', true) // json cookie, returns json object
Delete a cookie:
deleteCookie('name'); // json or string cookie: no matter!
Setting cookie options
For setting a cookie option, call setCookie(name, value, dict)
function filling dict parameter as dictionary.
Keys & values list:
- path (string) - ;
- domain (string) - ;
- expires (Date or string) - ;
- max-age (number) - ;
- secure (none) - ;
- samesite (string) - ;
- httpOnly (string) - .