Skip to content

Instantly share code, notes, and snippets.

@BigglesZX
Created December 5, 2013 11:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigglesZX/7803651 to your computer and use it in GitHub Desktop.
Save BigglesZX/7803651 to your computer and use it in GitHub Desktop.
HTML5 LocalStorage polyfill for IE7 using the userData behaviour
// if localStorage isn't available but IE's userData API is, polyfill it
// polyfill pattern based on https://gist.github.com/juliocesar/926500
if (Modernizr && !Modernizr.localstorage) {
// set up storage element
var storageNamespace = 'localStoragePolyfill',
storage = document.createElement('div');
if (typeof storage.addBehavior !== 'undefined') {
storage.id = '_storage';
storage.style.display = 'none';
storage.style.behavior = 'url("#default#userData")';
document.body.appendChild(storage);
storage.load(storageNamespace);
window.localStorage = {
getItem : function(id) { return storage.getAttribute(id) || undefined; },
setItem : function(id, val) { storage.setAttribute(id, val); storage.save(storageNamespace); },
removeItem : function(id) { storage.removeAttribute(id); storage.save(storageNamespace); },
clear : function() { return false; } // no way to appropriately polyfill this
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment