Skip to content

Instantly share code, notes, and snippets.

@simongong
Created August 11, 2014 10:37
Show Gist options
  • Save simongong/4f9489fbcf32089553dc to your computer and use it in GitHub Desktop.
Save simongong/4f9489fbcf32089553dc to your computer and use it in GitHub Desktop.
JavaScript: accessCookie()
var getCookie = function(name) {
var cookies = document.cookie.split(";");
for (var i=0; i < cookies.length; i++) {
var x = cookies[i].substr(0, cookies[i].indexOf("="));
var y = cookies[i].substr(cookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g,"");
if (x == name) {
return decodeURIComponent(y);
}
}
return null;
};
var setCookie = function(name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
value = encodeURIComponent(value) + (exdays == null ? '' : ('; expires=' + exdate.toUTCString()));
document.cookie = name + '=' + value + '; path=/;';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment