3

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?

alexander farkas
  • 13,754
  • 4
  • 40
  • 41
Andrew M. Andrews III
  • 1,989
  • 18
  • 23

2 Answers2

2

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

Andrew M. Andrews III
  • 1,989
  • 18
  • 23
-1

IE6 doesn't support HTML5 features at all. This is not very surprising for an ancient browser that should already be dead and buried (IE6 was released in the year 2001, and the foundations for HTML5 were only laid in 2004). See this answer for more details.

Note that there are wrappers which are capable of emulating such functionality - e.g. this question suggests jStorage for compatibility with IE6+.

Community
  • 1
  • 1
Piskvor left the building
  • 91,498
  • 46
  • 177
  • 222
  • 1
    I know this. The webshim polyfill is intended to provide a fallback in IE6 and other non-supporting browsers. – Andrew M. Andrews III May 20 '11 at 15:35
  • @Andrew M. Andrews III: Wonderful. What other relevant information you *know*, yet omitted from the question? (The readers are supposed to infer from thin air, because *of course* everyone knows *this* compatibility shim.) See, this kind of changes the whole question from "why doesn't IE6 support localStorage?" to "why is this localstorage shim not working?" - a completely different issue. – Piskvor left the building May 20 '11 at 15:39
  • My apologies, I thought that the title conveyed that the problem was with the webshim polyfill. – Andrew M. Andrews III May 22 '11 at 23:41