Set, get and remove cookies.
We ship cookiejs.js primarly as a UMD module but there is also an ES6 module included.
import cookiejs from 'cookiejs.js'; //should import cookiejs.es.js
cookiejs.set('testcookie', 'testvalue');
There is more than one way to use cookiejs.js inside your project. I prefer using npm for dependency management.
If you haven't used npm (Node Package manager) before, be sure to check out the Getting Started guide, as it explains how to install and use npm. Once you're familiar with that process, you may install the cookiejs.js module with this command inside your project:
npm install cookiejs.js --save-dev
Once the module has been installed, you may integrate that file into your build process (e.g concatenating and uglifying your JS with Grunt or whatever) since the --save-dev
option is meant for development only.
Sets a cookie.
// make sure cookiejs.js is already available when this code runs
cookiejs.set(
// name of the cookie
// default: undefined
// required
'captainObvious',
// value of the cookie
// default: ''
'Thank you Captain Obvious, you just saved my life.',
{
// specifies allowed hosts to receive the cookie
// default: the host of the current document location
domain: '.eric-zieger.de',
// indicates a URL path that must exist in the requested URL
// default: '/',
path: '/the-adventures-of-captain-obvious/',
// cookie is deleted when the client shuts down (session-cookie) or when the expire date is reached
// default: session-cookie
expires: new Date('2040-01-01').toUTCString(),
// cookie expires after a specific length of time in seconds
// default: undefined
'max-age': '3600',
// cookie is only sent to the server with a encrypted request over the HTTPS protocol
// default: false
secure: true
}
);
Get the value of a cookie.
// make sure cookiejs.js is already available when this code runs
cookiejs.get('captainObvious');
// returns 'Thank you Captain Obvious, you just saved my life.'
Removes a cookie by overwriting the expires date. When a custom domain and/or path atrribute is used you have to hand them as object into this function. Else the cookie will not get removed.
// make sure cookiejs.js is already available when this code runs
cookiejs.remove('captainObvious', {
domain: '.eric-zieger.de',
path: '/the-adventures-of-captain-obvious/'
});