问题描述
我正在尝试使用 HTML5 功能 localStorage
.根据本博客 它可以使用 IE8 完成,但是当我尝试使用它时,我收到一个 javascript 错误 'localStorage is null or not an object'
I am trying to use the HTML5 feature localStorage
. According to this blog it can be done using IE8, however when I try to use it I get a javascript error 'localStorage is null or not an object'
所以我的问题是:IE8 可以开箱即用地使用 localStorage
吗?这是我的代码:
So my question: can localStorage
be used by IE8 out-of-the-box? Here is my code:
<!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>
推荐答案
它确实支持 localStorage
,但你需要在 IE8 模式下(这在 IE7 模式下不起作用).
It does support localStorage
, though you need to be in IE8 mode (this will not work in IE7 mode).
要检查您是否在 IE8 模式下工作,请加载开发者控制台.在顶部,确保选择了 IE8 模式.标准模式也不错.
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.
您还想确定的一件事是您使用的是 HTML5 文档类型.您不应该使用具有 HTML5 功能的 XHTML 文档类型.
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.
<!DOCTYPE html>
使用此文档类型不应影响您的浏览器支持.
Using this doctype should not impact your browser support.
另外,请确保您访问 window.localStorage
.这应该不是问题,但众所周知 IE 会托管更奇怪的问题.也许它正在寻找一个本地范围的 localStorage
对象?谁知道呢.
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 localStorage
object? Who knows.
这篇关于IE8 开箱即用是否支持“localStorage"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!