3

当我在 IETester IE6 窗口中运行以下代码时:

<!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>

我在弹出对话框中收到以下错误:

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

该代码在以 IE7 模式运行的 IE9 中运行良好。

当我改用 Douglas Crockford 的JSON2.js和 Remy Sharp 的存储polyfill 时——这应该是基于它的——我没有问题。

请帮忙?

4

2 回答 2

2

我收到了作者 (Alexander Farkas) 的一封电子邮件,解释说使用 polyfill 的代码必须在 domready 事件处理程序中,例如:

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

更多信息: http ://afarkas.github.com/webshim/demos/index.html#polyfill-ready

于 2011-05-26T03:40:59.570 回答
-1

IE6根本不支持 HTML5 功能。对于一个应该已经死去并被埋葬的古老浏览器来说,这并不奇怪(IE6 于 2001 年发布,HTML5 的基础仅在 2004 年奠定)。有关更多详细信息,请参阅此答案

请注意,有些包装器能够模拟此类功能 - 例如,此问题建议jStorage与 IE6+ 兼容。

于 2011-05-20T15:30:21.147 回答