Skip to content

Instantly share code, notes, and snippets.

@aheinze
Created September 6, 2012 10:11
Show Gist options
  • Save aheinze/3654347 to your computer and use it in GitHub Desktop.
Save aheinze/3654347 to your computer and use it in GitHub Desktop.
Local+Session sorage wrapper
;(function(ls, ss){
var fallback = {setItem:function(){}, getItem:function(){ return null;}, removeItem:function(){}},
ls = ls || fallback},
ss = ss || fallback,
JSON = JSON || {stringify:function(){},parse:function(){}};
window.Store = {
"set": function(key, value) {
ls.setItem(key, JSON.stringify(value));
},
"get": function(key, def) {
var val = ls.getItem(key);
return val ? JSON.parse(val):def;
},
"remove": function() {
ls.removeItem(key);
},
"persist": function(key, value) {
ss.setItem(key, JSON.stringify(value));
},
"remember": function(key, def) {
var val = ss.getItem(key);
return val ? JSON.parse(val):def;
},
"forget": function(key) {
ss.removeItem(key);
}
};
})(window["localStorage"], window["sessionStorage"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment