简体   繁体   English

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

[英]“'localStorage' is null or not an object” Error in IE8

I have this string which i'm trying to store and get to localStorage, and retrieve from it so it could show. 我有这个字符串,我正在尝试存储并到达localStorage,并从中检索它以便显示出来。 Here's my code: 这是我的代码:

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'为null或不是对象”。

What's interesting is that it doesn't give 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... Iv'e还尝试使用“ 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? 根据Microsoft和W3的说法,IE8应该支持localStorage,那么有人对问题出在哪里有任何线索吗? Thanks a million! 太感谢了!

EDIT - This is a jsfiddle for the code. 编辑 - 是代码的jsfiddle。 for some reason, it doesn't work that good but just to give you a feel of the code... 由于某种原因,它不能很好地工作,而只是让您感觉到代码...

As per my understanding IE8 give storage to only valid domains. 根据我的理解,IE8仅将存储空间提供给有效域。 Try placing your example in some Web-server it should resolve the issue. 尝试将您的示例放在某些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)时,它运行良好。

Check if you are actually in IE 8 mode - as opposed to quirks or IE 7 mode. 检查您是否实际上处于IE 8模式-与怪癖或IE 7模式相对。 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. 最快的方法是按F12调出dev工具,浏览器模式在该选项卡的右上方列出。

I would give using window.localStorage as shot. 我会给使用window.localStorage作为镜头。 See Introduction to Web Storage for IE 请参阅IE的Web存储简介

Can you open the developer tools in IE and check that typeof json. 能否在IE中打开开发人员工具并检查json的类型。 stringify and json.parse are functions and also localstorage. stringify和json.parse是函数,也是localstorage。 I am not sure whether native JSON exist on IE. 我不确定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/2 and everything seems to work fine. [编辑]为您的代码jsfiddle.net/yrhdN/2添加了这个小提琴,并且一切正常。 I have tested in IE9 under IE8 compatibility mode) 我已经在IE8兼容模式下的IE9中进行了测试)

[Edit] One more thing about this code, it seems innerHtml in showStuff() doesn't work with a paragraph. [编辑]关于此代码的另一件事,似乎showStuff()中的innerHtml不适用于段落。 Changing the html from p to div and using innerText makes things a little better: 将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. 这似乎仅在IE中发生。 Here is an updated fiddle: http://jsfiddle.net/yrhdN/7/ 这是更新的小提琴: http : //jsfiddle.net/yrhdN/7/

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 在脚本中查找代码: window['localStorage'] !== null
  • change it to : window['localStorage'] != null 将其更改为: window['localStorage'] != null

It worked in my case. 就我而言,它奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM