Created
April 12, 2017 22:14
-
-
Save SparK-Cruz/08a96e81bbc48de51acb0fa7d07446cd to your computer and use it in GitHub Desktop.
A tiny lib for managing cookies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(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; | |
} | |
}; | |
} | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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();