Skip to content

Instantly share code, notes, and snippets.

@astromac
Last active August 29, 2015 14:07
Show Gist options
  • Save astromac/9eebc13be2ab4afde590 to your computer and use it in GitHub Desktop.
Save astromac/9eebc13be2ab4afde590 to your computer and use it in GitHub Desktop.
Get / Set Cookie
var setCookie = function (cookieName, cookieValue, expireDays) {
var date = new Date(),
expires = '';
date.setTime(date.getTime() + (expireDays * 24 * 60 * 60 * 1000));
expires = 'expires=' + date.toUTCString();
document.cookie = cookieName + '=' + cookieValue + '; ' + expires + '; ' + 'domain=' + document.location.hostname + '; path=/';
};
var getCookie = function(cookieName) {
var name = cookieName + '=',
cookieArray = document.cookie.split(';');
for (var i = 0; i < cookieArray.length; i++) {
var cookieValue = cookieArray[i];
while (cookieValue.charAt(0) === ' ') {
cookieValue = cookieValue.substring(1);
}
if (cookieValue.indexOf(name) !== -1) {
return { cookieOutput: cookieValue.substring(name.length, cookieValue.length) };
}
}
return false;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment