Snippets

Gravitywell BiscuitJS micro cookies library

Created by Lucian Buzzo last modified
;(function(){
function cookiesToObj(){ 
    var obj = {};
    document.cookie.split('; ').forEach(function(v) {
        var x = v.split('=');
        obj[decodeURIComponent(x[0])] = decodeURIComponent(x[1]);
    });
    return obj;
}
this.biscuit = {
    get: function(key){
        var cookies = cookiesToObj();
        return (cookies.hasOwnProperty(key)?cookies[key]:false);
    },
    set: function(key, val, forever) {
        document.cookie = encodeURIComponent(key) + '=' + encodeURIComponent(val)
        + '; domain=' + window.location.host + '; path=/'
        + (forever?'; expires=Fri, 31 Dec 9999 23:59:59 GMT':'');
    },
    remove: function(key) {
        document.cookie = encodeURIComponent(key) + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=' + window.location.host + '; path=/';
    }
}       
}.call(this));
(function(){function o(){var o={};return document.cookie.split("; ").forEach(function(n){var e=n.split("=");o[decodeURIComponent(e[0])]=decodeURIComponent(e[1])}),o}this.biscuit={get:function(n){var e=o();return e.hasOwnProperty(n)?e[n]:!1},set:function(o,n,e){document.cookie=encodeURIComponent(o)+"="+encodeURIComponent(n)+"; domain="+window.location.host+"; path=/"+(e?"; expires=Fri, 31 Dec 9999 23:59:59 GMT":"")},remove:function(o){document.cookie=encodeURIComponent(o)+"=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain="+window.location.host+"; path=/"}}}).call(this);

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.