Skip to content

Instantly share code, notes, and snippets.

@jaggy
Created May 17, 2014 12:34
Show Gist options
  • Save jaggy/07054623ff1afb1a27f0 to your computer and use it in GitHub Desktop.
Save jaggy/07054623ff1afb1a27f0 to your computer and use it in GitHub Desktop.
var Cookie = {
/**
* Create a cookie
*
* @param {string} name
* @param {mixed} value
* @param {string} days optional value
*/
set: function(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
// calculate the days into seconds... mili seconds?
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
document.cookie = name + "=" + value + expires + "; path=/";
},
/**
* Get the cookie value
* @param {string} name
* @return {mixed}
*/
get: function(name) {
var start;
var end;
if(document.cookie.length <= 0) {
return "";
}
start = document.cookie.indexOf(name + "=");
if (start == -1) {
return "";
}
start = start + name.length + 1;
end = document.cookie.indexOf(";", start);
if (end == -1) {
end = document.cookie.length;
}
return unescape(document.cookie.substring(start, end));
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment