Skip to content

Instantly share code, notes, and snippets.

@billiegoose
Created January 24, 2015 02:23
Show Gist options
  • Save billiegoose/7256c66c4e28f02c018e to your computer and use it in GitHub Desktop.
Save billiegoose/7256c66c4e28f02c018e 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);
};
Copy link

ghost commented Feb 4, 2018

👍

@dmitrysidorenko
Copy link

Thanks, man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment