“'localStorage'为空或不是对象” IE8中出错 [英] "'localStorage' is null or not an object" Error in IE8

查看:820
本文介绍了“'localStorage'为空或不是对象” IE8中出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个字符串,我正在尝试存储并转到localStorage,并从中检索以便显示。
这是我的代码:

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:

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;
}

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

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

有趣的是,当我打电话时没有给我一个错误'pushArray',它也使用'localStorage'。

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

我也试过使用window.localStorage而不仅仅是localStorage,它返回了同样的错误...

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

根据微软和W3的说法,IE8应该支持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!

编辑 - 这是一个代码的jsfiddle。出于某种原因,它没有那么好用,只是为了让你感觉一下代码...

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

推荐答案

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

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

当我将其作为单个文件进行测试时我遇到了同样的问题但是当我将它放在一个服务器(在我的情况下是Tomcat)它运行正常。

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.

这篇关于“'localStorage'为空或不是对象” IE8中出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆