【问题标题】:'Storage' is undefined in IE9 [duplicate]IE9中未定义“存储”[重复]
【发布时间】:2012-12-14 01:35:43
【问题描述】:

可能重复:
localStorage object is undefined in IE

下面的代码在 IE9 中给了我SCRIPT5009: 'Storage' is undefined。它适用于 Chrome、Safari、Firefox。

Storage.prototype.setObject = function(key, value) {
    this.setItem(key, JSON.stringify(value));
}

udpate我从 Apache 服务器提供页面,而不是从本地文件系统访问它,因此以下内容不适用:

localStorage object is undefined in IE

local storage in IE9 fails when the website is accessed directly from the file system

update 2发现问题,看我的回答。

【问题讨论】:

  • 页面上有 DOCTYPE 吗?页面是否在“兼容模式”下运行?
  • 我没有指定 DOCTYPE。它正在运行“怪癖”模式
  • @dev.e.loper:这可能就是问题所在。它需要在“IE9模式”下运行。
  • 我从 Apache 服务器提供页面,而不是从本地文件系统访问它

标签: javascript html


【解决方案1】:

发现问题。我必须指定 DOCTYPE <!DOCTYPE html> 即使其他浏览器不关心 IE。

感谢@Rocket Hazmat 指出这一点。

没有 DOCTYPE,IE 会进入 Quirks 模式。使用 DOCTYPE,IE 进入标准模式。我的猜测是 IE 需要处于标准模式才能访问 HTML5 功能。

【讨论】:

    【解决方案2】:

    在这里猜...

    在本地文件系统上运行时,IE 9 似乎不支持 localStorage:local storage in IE9 fails when the website is accessed directly from the file system

    更多细节可以在这里找到:http://www.wintellect.com/CS/blogs/jprosise/archive/2011/03/10/using-html5-web-storage-for-interprocess-communication.aspx

    因此,您可以在外部服务器上运行它 - 或检查您的脚本是否支持它:

    if ("localStorage" in window && window["localStorage"] != null) {
    
        // Local storage supported
    
    }
    

    【讨论】:

    • 我从 Apache 服务器提供页面,而不是从本地文件系统访问它。另外,我相信IE9支持存储caniuse.com/#feat=namevalue-storage
    • 是的!但它似乎不在本地文件系统上。这是我的猜测;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多