Created
February 27, 2014 23:34
-
-
Save andrewchilds/9262017 to your computer and use it in GitHub Desktop.
Simple localStorage polyfill, based on https://gist.github.com/juliocesar/926500.
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 () { | |
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