title: localStorage fallback attempt on file:// protocol
body: <p>I am working on an offline website that based on <code>js</code> and <code>html</code> (of course), so it's being served via the <code>file:// protocol</code>.</p>
  
  <p>In the website there's an option to mark items and add them to a list (like a "cart"). If it was an online version - I would use cookies and ajax/php. Since I can't use cookies (HTTP protocol), I found the <code>localStorage</code> object which fits to my requirements.</p>
  
  <p>However, it seems that <code>localStorage</code> isn't really working on IE with file:// protocol, while on Chrome it does. (Like there aren't enough reasons to hate IE). And this is where my journey for localStorage alternative/fallback begin.</p>
  
  <p>My fallback is to use the <code>window.name</code> to <a href="http://www.sitepoint.com/cookieless-javascript-session-variables/" rel="nofollow">store data</a>.</p>
  
  <p>My code:</p>
  
  <pre><code>//Modernizr approach - is localStorage being supported?
  function supportLocalStorage(){
      var test = 'test';
      try {
          localStorage.setItem(test, test);
          localStorage.removeItem(test);
          return true;
      } catch(e) {
          return false;
      }
  }
  
  $(document).ready(function(){
      if(supportLocalStorage() !== true){
          alert("boo"); 
          var localStorage = {
              getItem: function (key) {
                  if(window.name == null || window.name == "")
                      return "{}"; //Later i'm about to use JSON Parser.
                  return window.name;
              },
              setItem: function (key, val) {
                  window.name = val;
              }
          }
  
      }
  
      var products_list = localStorage.getItem("products");
  }
  </code></pre>
  
  <p>But now it seems that on IE everything is ok (no error), but on Chrome it gives the following error: (relates to the <code>var products_list</code> line)</p>
  
  <blockquote>
    <p>Uncaught TypeError: Cannot read property 'getItem' of undefined</p>
  </blockquote>
  
  <p>And if I remove the class declaration code-block of <code>localStorage = {..</code> it works again on Chrome but not on IE. Why does it happen? Am I missing something?</p>
  
  <p>Any suggestion/tip/help would be appreciated.</p>