Skip to content

Instantly share code, notes, and snippets.

@sebnilsson
Created March 20, 2013 14:34
Show Gist options
  • Save sebnilsson/5205125 to your computer and use it in GitHub Desktop.
Save sebnilsson/5205125 to your computer and use it in GitHub Desktop.
Get/set cookies
var cookies = {
setItem: function(name, value, days) {
var date = new Date();
date.setDate(date.getDate() + days);
var cookieValue = escape(value) + ((days === null) ? '' : '; expires=' + date.toUTCString());
document.cookie = name + '=' + cookieValue;
},
getItem: function(name) {
var i, key, value, cookieList = document.cookie.split(';'), item, splitIndex;
for (i = 0; i < cookieList.length; i++) {
item = cookieList[i];
splitIndex = item.indexOf('=');
key = item.substr(0, splitIndex);
value = item.substr(splitIndex + 1);
key = key.replace(/^\s+|\s+$/g, '');
if (key === name) {
return unescape(value);
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment