Skip to content

Instantly share code, notes, and snippets.

@jstefek
Created September 7, 2016 11:06
Show Gist options
  • Save jstefek/4fa9a32107e0f4984ade96beea99ce70 to your computer and use it in GitHub Desktop.
Save jstefek/4fa9a32107e0f4984ade96beea99ce70 to your computer and use it in GitHub Desktop.
var CookieHandler = {
createCookie: function (name, value, days) {
console.log("create cookie: " + name + ", value: " + value);
var expires;
if (!days) {
days = 1000;
}
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
},
getCookie: function (name) {
if (document.cookie.length > 0) {
var start = document.cookie.indexOf(name + "=");
if (start !== -1) {
start = start + name.length + 1;
var end = document.cookie.indexOf(";", start);
if (end === -1) {
end = document.cookie.length;
}
return unescape(document.cookie.substring(start, end));
}
}
return "";
},
deleteCookie: function (name) {
document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment