Skip to content

Instantly share code, notes, and snippets.

@trekawek
Last active December 10, 2015 22:18
Show Gist options
  • Save trekawek/4501005 to your computer and use it in GitHub Desktop.
Save trekawek/4501005 to your computer and use it in GitHub Desktop.
var api = {};
api.Cookie = {
create: function (name, value, days) {
var expires = "",
date;
if (days) {
date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
document.cookie = name + "=" + value + expires + "; path=/";
},
read: function (name) {
var nameEQ = name + "=",
ca = document.cookie.split(';'),
c = null,
i;
for (i = 0; i < ca.length; i++) {
c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1, c.length);
}
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
},
erase: function (name) {
api.cookie.create(name, "", -1);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment