Skip to content

Instantly share code, notes, and snippets.

@SparK-Cruz
Created April 12, 2017 22:14
Show Gist options
  • Save SparK-Cruz/08a96e81bbc48de51acb0fa7d07446cd to your computer and use it in GitHub Desktop.
Save SparK-Cruz/08a96e81bbc48de51acb0fa7d07446cd to your computer and use it in GitHub Desktop.
A tiny lib for managing cookies
;(function(){
// Cookie management
window.Furnace = {
cookie: function(name) {
function splitCookie() {
if (document.cookie.length == 0) {
return null;
}
var start = document.cookie.indexOf(name + "=");
if (start == -1) {
return null;
}
var 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 {
name: name,
bake: function(value) {
document.cookie = this.name + "=" + value + "; path=/";
},
bite: function() {
return splitCookie();
},
burn: function() {
return this.bake(';expires=Thu, 01 Jan 1970 00:00:01 GMT');
},
eat: function() {
var result = this.bite();
this.burn();
return result;
}
};
}
};
})();
@SparK-Cruz
Copy link
Author

Usage

Create or update cookie:
Furnace.cookie(name).bake(value);

Destroy/expire cookie
Furnace.cookie(name).burn();

Get cookie value
var storedValue = Furnace.cookie(name).bite();

Get cookie value and destroy it
var storedValue = Furnace.cookie(name).eat();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment