Last active
August 29, 2015 14:17
-
-
Save ajagelund/ba1d8ac95db33ed99807 to your computer and use it in GitHub Desktop.
Polyfill fill for localStorage and sessionStorage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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!'); | |
} | |
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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