javascript “'localStorage' 为空或不是对象” IE8 中的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12772010/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 17:04:59  来源:igfitidea点击:

"'localStorage' is null or not an object" Error in IE8

javascripthtmlinternet-explorer-8local-storage

提问by Tomcatom

I have this string which i'm trying to store and get to localStorage, and retrieve from it so it could show. Here's my code:

我有这个字符串,我正在尝试存储并访问 localStorage,然后从中检索以显示它。这是我的代码:

var datas = new Array;
if (navigator.appName !== 'Microsoft Internet Explorer'){
    var qsVal = document.getElementsByClassName("val");
}
else{
    var qsVal = document.querySelectorAll('.val');
}
if (navigator.appName !== 'Microsoft Internet Explorer'){
    var qsKey = document.getElementsByClassName("key");
}
else{
    var qsKey = document.querySelectorAll('.key');
}
var storedPlays;
var stuff = document.getElementById("stuff");

function pushArray(){
    for (var i=0, len = qsVal.length; i < len; i++){
    thisValue = qsVal[i].value;
    thisKey = qsKey[i].value;
    datas.push([thisValue,thisKey]);
    }
localStorage.setItem('datas', JSON.stringify(datas));
}

function showStuff(){
    storedPlays = JSON.parse(localStorage.getItem('datas'));
    document.getElementById("stuff").innerHTML = storedPlays;
}

It works great with FF and Chrome, but IE8 returns "'localStorage' is null or not an object" when I call 'showStuff'.

它适用于 FF 和 Chrome,但当我调用 'showStuff' 时,IE8 返回“'localStorage' 为空或不是对象”。

What's interesting is that it doesn'tgive me an error when I call 'pushArray', which uses 'localStorage' as well.

有趣的是,当我调用“pushArray”时它不会给我一个错误,它也使用“localStorage”。

Iv'e also tried using "window.localStorage" instead of just "localStorage", it returned the same error...

我还尝试使用“window.localStorage”而不仅仅是“localStorage”,它返回了相同的错误......

IE8 is supposed to support localStorage, according to Microsoft and W3, so does anyone has any clue as to where the problem is? Thanks a million!

根据微软和 W3 的说法,IE8 应该支持 localStorage,那么有人知道问题出在哪里吗?太感谢了!

EDIT- Thisis a jsfiddle for the code. for some reason, it doesn't work that good but just to give you a feel of the code...

编辑-是代码的 jsfiddle。出于某种原因,它并没有那么好,只是为了让您了解代码......

回答by darwinbaisa

As per my understanding IE8 give storage to only valid domains. Try placing your example in some Web-server it should resolve the issue.

根据我的理解,IE8 只为有效域提供存储空间。尝试将您的示例放在某个 Web 服务器中,它应该可以解决问题。

I faced the same issue when I tested it as an individual file but when i placed it in a server(Tomcat in my case) it just worked fine.

当我将它作为单个文件进行测试时,我遇到了同样的问题,但是当我将它放在服务器(在我的例子中是 Tomcat)中时,它运行良好。

回答by Patrick

Check if you are actually in IE 8 mode - as opposed to quirks or IE 7 mode. Fastest way to do this is hit F12 to bring up the dev tools, and the browser mode is listed on the upper right of that tab.

检查您是否真的处于 IE 8 模式 - 而不是 quirks 或 IE 7 模式。最快的方法是点击 F12 以打开开发工具,浏览器模式列在该选项卡的右上角。

回答by krg

I would give using window.localStorageas shot. See Introduction to Web Storagefor IE

我会使用window.localStorage作为镜头。请参阅IE Web 存储简介

回答by kabaros

Can you open the developer tools in IE and check that typeof json. stringify and json.parse are functions and also localstorage. I am not sure whether native JSON exist on IE.

你能在IE中打开开发者工具并检查json的类型吗?stringify 和 json.parse 是函数,也是 localstorage。我不确定 IE 上是否存在原生 JSON。

Also why are you setting the object inside the loop, shouldnt it be outside it?

另外,为什么要在循环内设置对象,不应该在循环之外?

[Edit]Added this fiddle for your code jsfiddle.net/yrhdN/2and everything seems to work fine. I have tested in IE9 under IE8 compatibility mode)

[编辑]为您的代码jsfiddle.net/yrhdN/2添加了这个小提琴,一切似乎都正常。我在IE8兼容模式下在IE9中测试过)

[Edit]One more thing about this code, it seems innerHtml in showStuff() doesn't work with a paragraph. Changing the html from p to div and using innerText makes things a little better:

[编辑]关于这段代码的另一件事,showStuff() 中的innerHtml 似乎不适用于段落。将 html 从 p 更改为 div 并使用 innerText 使事情变得更好:

<div id="stuff">
</div>


function showStuff(){
    var storedPlays    = JSON.parse(localStorage.getItem('datas'));
    document.getElementById("stuff").innerText = storedPlays;
}

This seems to happen only in IE. Here is an updated fiddle: http://jsfiddle.net/yrhdN/7/

这似乎只发生在 IE 中。这是一个更新的小提琴:http: //jsfiddle.net/yrhdN/7/

回答by James.B

Try this also if you do not wish any local server application to shoot the webpage.

如果您不希望任何本地服务器应用程序拍摄网页,也可以尝试此操作。

  • Look for the code in your script : window['localStorage'] !== null
  • change it to : window['localStorage'] != null
  • 在脚本中查找代码: window['localStorage'] !== null
  • 将其更改为: window['localStorage'] != null

It worked in my case.

它在我的情况下有效。