Skip to content

Instantly share code, notes, and snippets.

@jsocol
Created March 18, 2011 14:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsocol/876117 to your computer and use it in GitHub Desktop.
Save jsocol/876117 to your computer and use it in GitHub Desktop.
Simple namespaced storage API for JS (largely stolen from Chris Van)
/**
* storage.js - Simple namespaced browser storage.
*
* Creates a window.Storage function that gives you an easy API to access localStorage,
* with fallback to cookie storage. Each Storage object is namespaced:
*
* var foo = Storage('foo'), bar = Storage('bar');
* foo.set('test', 'A'); bar.set('test', 'B');
* foo.get('test'); // 'A'
* bar.remove('test');
* foo.get('test'); // still 'A'
*
* Requires jQuery.
* Based on https://github.com/jbalogh/zamboni/blob/master/media/js/zamboni/storage.js
* Everything clever written by Chris Van.
*/
var Storage = (function () {
var cookieStorage = {
expires: 30,
getItem: function (key) {
return $.cookie(key);
},
setItem: function (key, value) {
return $.cookie(key, value, {path: "/", expires: this.expires});
},
removeItem: function (key) {
return $.cookie(key, null);
}
};
var engine = cookieStorage;
try {
if ("localStorage" in window && window["localStorage"] !== null) {
engine = window.localStorage;
}
} catch (e) {
}
return function (namespace) {
if (!namespace) {
namespace = '';
}
return {
get: function (key) {
return engine.getItem(namespace + "-" + key);
},
set: function (key, value) {
return engine.setItem(namespace + "-" + key, value);
},
remove: function (key) {
return engine.remoteItem(namespace + "-" + key);
}
}
}
})();
@ramoncaldeira
Copy link

the remove function has a misspelling. It is returning engine.remoTe, instead of engine.remoVe

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