Skip to content

Instantly share code, notes, and snippets.

@ajagelund
Last active August 29, 2015 14:17
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 ajagelund/ba1d8ac95db33ed99807 to your computer and use it in GitHub Desktop.
Save ajagelund/ba1d8ac95db33ed99807 to your computer and use it in GitHub Desktop.
Polyfill fill for localStorage and sessionStorage
(function(){
try {
window.__ls = {};
window.localStorage.setItem = function (id, val) { return window.__ls[id] = String(val); };
window.localStorage.getItem = function (id) { return window.__ls.hasOwnProperty(id) ? window.__ls[id] : undefined; };
window.localStorage.removeItem = function (id) { return delete window.__ls[id]; };
window.localStorage.clear = function () { return window.__ls = {} };
console.log('localStorage lap of luxury!');
} catch (e) {
console.log('Reset localStorage failed!');
}
try {
window.__ss = {};
window.sessionStorage.setItem = function (id, val) { return window.__ss[id] = String(val); };
window.sessionStorage.getItem = function (id) { return window.__ss.hasOwnProperty(id) ? window.__ss[id] : undefined; };
window.sessionStorage.removeItem = function (id) { return delete window.__ss[id]; };
window.sessionStorage.clear = function () { return window.__ss = {} };
console.log('sessionStorage success!');
} catch (e) {
console.log('Reset sessionStorage failed!');
}
})();
if (!Modernizr.localstorage) {
var setTemporaryLocalStorage = function() {
try {
window.__ls = window.__ls || {};
window.localStorage.setItem = function (id, val) { return window.__ls[id] = String(val); };
window.localStorage.getItem = function (id) { return window.__ls.hasOwnProperty(id) ? window.__ls[id] : undefined; };
window.localStorage.removeItem = function (id) { return delete window.__ls[id]; };
window.localStorage.clear = function () { return window.__ls = {} };
return true;
} catch (e) {
return false;
}
};
Modernizr.localstorage = setTemporaryLocalStorage();
}
if (!Modernizr.sessionstorage) {
var setTemporarySessionStorage = function() {
try {
window.__ss = window.__ss || {};
window.sessionStorage.setItem = function (id, val) { return window.__ss[id] = String(val); };
window.sessionStorage.getItem = function (id) { return window.__ss.hasOwnProperty(id) ? window.__ss[id] : undefined; };
window.sessionStorage.removeItem = function (id) { return delete window.__ss[id]; };
window.sessionStorage.clear = function () { return window.__ss = {} };
return true;
} catch (e) {
return false;
}
};
Modernizr.sessionstorage = setTemporarySessionStorage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment