Created
          December 5, 2013 11:10 
        
      - 
      
- 
        Save BigglesZX/7803651 to your computer and use it in GitHub Desktop. 
    HTML5 LocalStorage polyfill for IE7 using the userData behaviour
  
        
  
    
      This file contains hidden or 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 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