Skip to content

Instantly share code, notes, and snippets.

@bbrewer97202
Created June 3, 2013 16:03
Show Gist options
  • Save bbrewer97202/5699204 to your computer and use it in GitHub Desktop.
Save bbrewer97202/5699204 to your computer and use it in GitHub Desktop.
cookie access via javascript starter
var cookie = {
//session cookies
setCookie: function(name, value) {
document.cookie = name + "=" + escape(value) + ';path=/';
},
getCookie: function(name) {
var key, val;
var cookies = document.cookie.split(";");
var cookiesLength = cookies.length;
for (var i=0; i < cookiesLength; i++) {
key = cookies[i].substr(0, cookies[i].indexOf("="));
key = key.replace(/^\s+|\s+$/g, "");
if (key === name) {
return cookies[i].substr(cookies[i].indexOf("=") + 1);
}
}
},
cookieExists: function(name) {
return (cookie.getCookie(name)) ? true : false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment