webshim polyfill localStorage在IE6中未定义 [英] webshim polyfill localStorage undefined in IE6
问题描述
当我在IETester 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
在IE7模式下运行的IE9中,代码运行正常。
The code works fine in IE9 running in IE7 mode.
当我改为使用Douglas Crockford的 JSON2.js 时和Remy Sharp的存储填充 - 这应该是基于 - 我没有问题。
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.
请帮忙?
推荐答案
我收到了作者的电子邮件( Alexander Farkas)解释使用polyfill的代码必须在domready事件处理程序中,如下所示:
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'));
});
更多信息:
http://afarkas.github.com/webshim/demos/index.html#polyfill-ready
这篇关于webshim polyfill localStorage在IE6中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!