Skip to content

Instantly share code, notes, and snippets.

@ivcreative
Forked from billiegoose/vanilla-cookies.js
Created November 18, 2020 15:58
Show Gist options
  • Save ivcreative/96a3b3d6078c0634d9768cac750ecac9 to your computer and use it in GitHub Desktop.
Save ivcreative/96a3b3d6078c0634d9768cac750ecac9 to your computer and use it in GitHub Desktop.
Vanilla Cookies - a dead simple yet full-featured JavaScript cookie library
// Vanilla Cookies - a dead simple yet full-featured JavaScript cookie library
// derived from http://stackoverflow.com/a/19189846
/*********************************************************
Gets the value of a cookie.
**********************************************************/
getCookie = function(sName)
{
var oCrumbles = document.cookie.split(';');
for(var i=0; i<oCrumbles.length;i++)
{
var oPair= oCrumbles[i].split('=');
var sKey = decodeURIComponent(oPair[0].trim());
var sValue = oPair.length>1 ? oPair[1] : '';
if(sKey == sName) {
return decodeURIComponent(sValue);
}
}
return '';
};
/*****************************************************************
Sets the value of a cookie. Expiration date and path are optional.
******************************************************************/
setCookie = function(sName, sValue, options)
{
//oDate.setYear(oDate.getFullYear()+1);
var sCookie = encodeURIComponent(sName) + '=' + encodeURIComponent(sValue);
// Shorthand: options === expires date
if (options && options instanceof Date) {
options = {
expires: options
};
}
// Longhand: options object
if (options && typeof options == 'object') {
if (options.expires) {
sCookie += '; expires=' + options.expires.toGMTString();
}
if (options.path) {
sCookie += '; path=' + options.path.toString();
}
if (options.domain) {
sCookie += '; domain=' + options.domain.toString();
}
if (options.secure) {
sCookie += '; secure';
}
}
document.cookie= sCookie;
};
/*********************************************************
Clears and immediately expires a cookie.
**********************************************************/
removeCookie = function(sName, options)
{
if (!options) {
var options = {};
}
options.expires = new Date();
setCookie(sName, '', options);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment