【问题标题】:localStorage not working with IE 10 throwing errorslocalStorage 不适用于 IE 10 引发错误
【发布时间】:2013-02-01 03:59:49
【问题描述】:

我正在使用 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 已启动”,其余警报未显示。

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

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

在我注释掉 localStorage 代码的那一刻,所有警报都会显示出来,但控制台中仍然存在一个错误。

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

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

编辑: 我的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>

【问题讨论】:

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


【解决方案1】:

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

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

更新:但这并不等同于本地存储,因为当页面刷新时它会被清除。进一步探索后发现 localStorage 不适用于 ie10 中的 file:// 协议。但是,如果您将页面托管在 Web 服务器(IIS/Tomcat 等)下并通过 http:// 协议访问它,localStorage 在 ie10 中就可以正常工作。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      您可能需要正确的文档类型:

      <!doctype html>
      

      【讨论】:

      • 是的,我正在使用它。我已经用我正在使用的 html 更新了我的问题;请看一看。谢谢
      • 看看六氰化物的答案——它可能对你来说是正确的。
      • 是的,这确实解决了我的第二个问题。但是在使用 localStorage 时仍然会出现相同的错误。如何解决这个问题?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-12
      • 2013-04-15
      • 2017-07-27
      • 1970-01-01
      • 2017-10-30
      • 2017-04-15
      相关资源
      最近更新 更多