webshim polyfill localStorage undefined in IE6

↘锁芯ラ 提交于 2019-12-22 11:27:02

问题


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?


回答1:


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




回答2:


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+.



来源:https://stackoverflow.com/questions/6074115/webshim-polyfill-localstorage-undefined-in-ie6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!