Javascript IE8 开箱即用是否支持“localStorage”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3452816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does IE8 out-of-the-box have support for 'localStorage'
提问by Spiderman
I am trying to use the HTML5 feature localStorage. According to this blogit can be done using IE8, however when I try to use it I get a javascript error 'localStorage is null or not an object'
我正在尝试使用 HTML5 功能localStorage。根据这个博客,它可以使用 IE8 来完成,但是当我尝试使用它时,我收到了一个 javascript 错误'localStorage is null or not an object'
So my question: can localStoragebe used by IE8 out-of-the-box? Here is my code:
所以我的问题是:localStorageIE8可以开箱即用吗?这是我的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=8" />
<title>IE8 - DOM Storage</title>
<script type="text/javascript">
function Save() {
localStorage.setItem('key','value');
}
</script>
</head>
<body>
<button onclick="Save();">
Save
</button>
</body>
</html>
回答by mattbasta
It does support localStorage, though you need to be in IE8 mode (this will not work in IE7 mode).
它确实支持localStorage,但您需要处于 IE8 模式(这在 IE7 模式下不起作用)。
To check that you're working in IE8 mode, load up the developer console. At the top, make sure that IE8 mode is selected. Standards mode would also be nice.
要检查您是否在 IE8 模式下工作,请加载开发者控制台。在顶部,确保选择了 IE8 模式。标准模式也不错。
One thing that you also want to make sure of is that you're using the HTML5 doctype. You shouldn't be able to use an XHTML doctype with HTML5 features.
您还想确保的一件事是您正在使用 HTML5 文档类型。您不应该使用具有 HTML5 功能的 XHTML 文档类型。
<!DOCTYPE html>
Using this doctype should not impact your browser support.
使用此文档类型不应影响您的浏览器支持。
Also, make sure you access window.localStorage. It shouldn't be an issue, but IE has been known to host weirder issues. Perhaps it's looking for a locally scoped localStorageobject? Who knows.
另外,请确保您访问window.localStorage. 这不应该是一个问题,但众所周知 IE 会托管更奇怪的问题。也许它正在寻找一个本地范围的localStorage对象?谁知道。
回答by Spiderman
the comment of musicfreak was correct. Because this feature requires domain, I had to use it only through a live url (at least localhost) and not by opening it as a file from a disk.
Musicfreak 的评论是正确的。由于此功能需要域,因此我只能通过实时 url(至少是 localhost)使用它,而不是将其作为磁盘文件打开。
There is no need to add window.localStorageas IE8 recognizes just localStorageas well.
不需要添加,window.localStorage因为 IE8 也可以识别localStorage。
回答by vernonner3voltazim
Expanding upon a previous good Answer:
扩展以前的好答案:
I had this same problem --worse! even IE10 failed!-- and I discovered the solution (for me) had more to do with my development environment than Internet Explorer. What I had done was create an HTML file, and edit it a lot using a good text editor. I could click-drag the file into a browser to see my progress in developing the page, and later simply refresh the browser window, when I updated/edited various things in the file.
我有同样的问题——更糟!甚至 IE10 也失败了!-- 我发现该解决方案(对我而言)与我的开发环境有关,而不是 Internet Explorer。我所做的是创建一个 HTML 文件,并使用一个好的文本编辑器对其进行大量编辑。我可以将文件单击并拖动到浏览器中以查看我开发页面的进度,然后当我更新/编辑文件中的各种内容时只需刷新浏览器窗口。
It turns out that for Chrome and Firefox and Opera, window.localStorage was a valid object, but for Internet Explorer, it was "undefined" (as seen in the debugger). However, once I fired up a Web Server program (for "localhost"), and used that to feed the HTML page to the browser, then Internet Explorer provided a valid object ("DispHTMLStorage") for window.localStorage. Also note that in Tools/Options/Advanced for IE, there is a checkbox "enable DOM storage" --it appears to be checked by default, but it is always possible that some user will have manually disabled it.
事实证明,对于 Chrome、Firefox 和 Opera,window.localStorage 是一个有效对象,但对于 Internet Explorer,它是“未定义的”(如调试器中所见)。然而,一旦我启动了一个 Web 服务器程序(对于“localhost”),并使用它来将 HTML 页面提供给浏览器,然后 Internet Explorer 为 window.localStorage 提供了一个有效的对象(“DispHTMLStorage”)。另请注意,在 IE 的工具/选项/高级中,有一个复选框“启用 DOM 存储”——它似乎是默认选中的,但总是有可能某些用户手动禁用它。
回答by foxontherock
Try using "IE=edge" instead of "IE=8" in your x-ua-compatible meta tag
From Microsoft: (link)
尝试在您的 x-ua 兼容元标记中使用“IE=edge”而不是“IE=8”
来自 Microsoft:(链接)
Use the following value to display the webpage in EdgeHTML mode, which is the highest standards mode supported by Internet Explorer, from Internet Explorer 6 through IE11.
使用以下值以 EdgeHTML 模式显示网页,EdgeHTML 模式是 Internet Explorer 支持的最高标准模式,从 Internet Explorer 6 到 IE11。
<meta http-equiv="x-ua-compatible" content="IE=edge" >
回答by Venkataramana Naik Palthya
Local storage concept should work on IE8+.
本地存储概念应该适用于 IE8+。
All you have to do is put your source code on the server and run it.
您所要做的就是将源代码放在服务器上并运行它。

