admin管理员组

文章数量:1278792

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

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

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

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

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!

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...

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

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

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

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

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!

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...

Share Improve this question edited Oct 7, 2012 at 20:02 Tomcatom asked Oct 7, 2012 at 19:19 TomcatomTomcatom 3653 gold badges6 silver badges16 bronze badges 3
  • 3 CanIUse. also thinks IE8 should support it. caniuse./#search=local – Spudley Commented Oct 7, 2012 at 19:23
  • I have added this jsfiddle: jsfiddle/yrhdN/2 (just changing little thigns to make your code work) and everything seems to work fine. I have tested in IE9 under IE8 patibility mode) – kabaros Commented Oct 7, 2012 at 20:32
  • Could it be something like document.querySelectorAll firing before the DOM is loaded in your application? – kabaros Commented Oct 7, 2012 at 20:42
Add a ment  | 

5 Answers 5

Reset to default 6

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

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.

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.

I would give using window.localStorage as shot. See Introduction to Web Storage for IE

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.

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

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

[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:

<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/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
  • change it to : window['localStorage'] != null

It worked in my case.

本文标签: javascriptquot39localStorage39 is null or not an objectquot Error in IE8Stack Overflow