Skip to content

Instantly share code, notes, and snippets.

@jethrolarson
Forked from furf/jQuery.storage.js
Last active December 11, 2015 03:08
Show Gist options
  • Save jethrolarson/4535157 to your computer and use it in GitHub Desktop.
Save jethrolarson/4535157 to your computer and use it in GitHub Desktop.
// Adds Support for localStorage to IE5-IE7 using userData
// <http://msdn.microsoft.com/en-us/library/ms531424(VS.85).aspx>
// See also <http://caniuse.com/#feat=namevalue-storage>
if(typeof window.localStorage === 'undefined') (function () {
var userdataKey = 'localStorage',
userdata = document.createElement('b');
userdata.style.display = 'none';
userdata.style.behavior = 'url("#default#userData")';
document.body.appendChild(userdata);
userdata.load(userdataKey);
window.localStorage = {
setItem: function (key, val) {
userdata.setAttribute(key, val);
userdata.save(userdataKey);
},
removeItem: function (key) {
userdata.removeAttribute(key);
userdata.save(userdataKey);
},
getItem: function (key) {
return userdata.getAttribute(key);
}
};
}
})();
@jethrolarson
Copy link
Author

This probably needs better error handling. This also doesn't make the localStorage api support object storage (JSON) which is a feature I love. This is just a simple polyfill so you have to make sure you're only storing strings. Also you have to use .getItem() and .setItem() or this polyfill wont work.

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