Skip to content

Instantly share code, notes, and snippets.

@andrewchilds
Created February 27, 2014 23:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewchilds/9262017 to your computer and use it in GitHub Desktop.
Save andrewchilds/9262017 to your computer and use it in GitHub Desktop.
Simple localStorage polyfill, based on https://gist.github.com/juliocesar/926500.
(function () {
function isSupported() {
try {
return 'localStorage' in window && window['localStorage'] !== null;
} catch(e) {
return false;
}
}
if (!isSupported()) {
function init(undef) {
var store = {
setItem: function (id, val) {
return store[id] = String(val);
},
getItem: function (id) {
return store.hasOwnProperty(id) ? String(store[id]) : undef;
},
removeItem: function (id) {
return delete store[id];
},
clear: function () {
init();
}
};
window.localStorage = store;
}
init();
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment