localStorage无法使用IE 10抛出错误

时间:2013-02-16 05:15:08

标签: javascript jquery windows-phone-8 local-storage internet-explorer-10

我正在使用HTML 5项目模板制作Windows 8 Phone应用程序。

我已经使用nuget下载了 jquery-1.9.1.js ,并且还从here下载了 jquery.mobile-1.2.0.js 将它们添加到我的项目中。

现在我使用以下代码,我正在尝试使用localStorage,我的下面的代码适用于Chrome和Firefox,但不适用于IE 10。

Common.js

$(document).ready(function(){
    alert("jquery started");
    localStorage.selectedCategory = "cc";
    alert("After assignment");
    alert(localStorage.selectedCategory);
})

当我在IE 10中运行时,我在控制台中收到以下错误消息,只显示第一个警告“jquery started”,其余警报未显示。

  

SCRIPT5007:无法获取未定义或空引用的属性“msie”       jquery.mobile-1.2.0.js,第2536行5字符

     

SCRIPT5007:无法设置未定义或空引用的属性“selectedCategory”       common.js,第3行第2个字符

enter image description here

当我注释掉localStorage代码时,会显示所有警报,但仍有一个错误仍在控制台中。

  

SCRIPT5007:无法获取未定义或空引用的属性“msie”   jquery.mobile-1.2.0.js,第2536行5字符

enter image description here

我似乎不明白这个问题,请帮我解决这个问题。

编辑: 我的HTML代码:

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <link rel="stylesheet" type="text/css" href="/html/css/phone.css" />
        <title>Interview QA</title>
        <script type="text/javascript" src="../Scripts/jquery-1.9.1.js"></script>
        <script type="text/javascript" src="../Scripts/jquery.mobile-1.2.0.js"></script>
        <script type="text/javascript" src="../Scripts/Custom/common.js"></script>
    </head>
    <body>
      <div id="header"></div>
      <div id="footer"></div>
      <div></div>
    </body>
</html>

3 个答案:

答案 0 :(得分:2)

您可能需要正确的doctype:

<!doctype html>

答案 1 :(得分:2)

我也得到了同样的错误。但是当我用关键字“Storage”替换“localStorage”时,它在ie10中工作。像这样修改你的代码并尝试:

$(document).ready(function(){
    alert("jquery started");
    Storage.selectedCategory = "cc";
    alert("After assignment");
    alert(Storage.selectedCategory); })

更新但这不等同于本地存储,因为在刷新页面时会清除此内容。进一步探索时发现localStorage在ie10中没有使用file:// protocol。但是,如果您在Web服务器(IIS / Tomcat等)下托管页面并通过http://协议访问它,则localStorage在ie10中工作正常。

答案 2 :(得分:0)

jquery已经删除了$ .browser和浏览器检查代码,所以请使用jquery migrate.js来避免这个问题。

http://blog.jquery.com/2013/01/15/jquery-1-9-final-jquery-2-0-beta-migrate-final-released/

相关问题