当我在 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/