Skip to content

Instantly share code, notes, and snippets.

@eastmedia
Created September 15, 2008 15:56
Show Gist options
  • Save eastmedia/10882 to your computer and use it in GitHub Desktop.
Save eastmedia/10882 to your computer and use it in GitHub Desktop.
// Cookie
var Cookie = {
set: function(name, value, daysToExpire) {
var expire = '';
if (daysToExpire != undefined) {
var d = new Date();
d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
expire = '; expires=' + d.toGMTString();
}
return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
},
get: function(name) {
var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
return (cookie ? unescape(cookie[2]) : null);
},
erase: function(name) {
var cookie = Cookie.get(name) || true;
Cookie.set(name, '', -1);
return cookie;
},
accept: function() {
if (typeof navigator.cookieEnabled === 'boolean') {
return navigator.cookieEnabled;
}
Cookie.set('_test', '1');
return (Cookie.erase('_test') === '1');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment