Search code examples
htmllocal-storagewebshim

webshim polyfill localStorage undefined in IE6


When I run the following code in an IETester IE6 window:

<!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <title>DealingTree</title>
        <meta http-equiv="Content-type" content="text/html;charset=utf-8"/>
        <script type="text/javascript" src="/js/modernizr.js"> </script>
        <script type="text/javascript" src="/js/jquery.js"> </script>
        <script type="text/javascript" src="/js/sssl.js"> </script>
        <script type="text/javascript" src="/js/webshims/js-webshim/minified/polyfiller.js"> </script>
      </head>
      <body>
        <script type="text/javascript">
          //<![CDATA[
          $.webshims.polyfill('json-storage');
          localStorage.setItem('myKey','myValue');
          alert(localStorage.getItem('myKey'));
          //]>
        </script>
      </body>
    </html>

I get the following error in a popup dialog:

Line:  15
Char:  7
Error: 'localStorage' is undefined
Code:  0
URL:   http://localhost/problem2.html

The code works fine in IE9 running in IE7 mode.

When I change to use Douglas Crockford's JSON2.js and Remy Sharp's storage polyfill --upon which this is supposedly based-- I do not have the problem.

Please help?


Solution

  • I received an email from the author (Alexander Farkas) explaining that the code using the polyfill must be inside a domready event handler, such as the following:

    $.webshims.polyfill('json-storage');
    $(function(){
      localStorage.setItem('myKey','myValue');
      alert(localStorage.getItem('myKey'));
    });
    

    For more information: http://afarkas.github.com/webshim/demos/index.html#polyfill-ready