html - webshim polyfill localStorage 在 IE6 中未定义

标签 html local-storage webshim

当我在 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 的 storage polyfill -- 据推测这是基于 -- 我没有问题。

请帮忙?

最佳答案

我收到一封来自作者 (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

关于html - webshim polyfill localStorage 在 IE6 中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6074115/

相关文章:

javascript - JWPlayer 自动检测每个用户的最佳质量,而不使用其他浏览器的 flash 播放器(HLS、.m3u8)?

javascript - 如果表单为空,则阻止提交表单

javascript - 使用 HTML5 localstorage 更新 json 键的值

javascript - 单击 jquery 添加属性 'checked'

javascript - Selenium htmlUnit 动态内容不起作用

javascript - 使用 Webshim 仅替换日期选择器的 UI

html - 我可以使用 HTML 输入类型 "date"只收集一年吗?

html 在 2 列布局中居中和调整图像大小

javascript - Angular js的单个复选框

返回上一页时,Javascript 不执行,本地和实时在 githubpage、edge 和 chrome 上的行为不同