Is "Localstorage" in Firefox Only Working When the Page Is Online

Is localStorage in Firefox only working when the page is online?

It seems a bug: Bug 507361 - localStorage doesn't work in file:/// documents
Hope is fixed soon!

2011-09-13: Bug fixed, implemented in 'Mozilla8'. I tested this with Firefox 8 and it works now.

In Firefox, localStorage data is not accessible by different windows in the same local domain. Is there a workaround?

This was previously reported and won't be fixed because that would create a security issue. It may not work in the future in other browsers. Serving the pages from a local server is suggested.

localStorage not working in IE9 and Firefox

The way you're checking to see if localstorage exists is a bit unorthodox and may actually fail to detect that a legacy browser doesn't have localstorage. Below, you have typos in this code, such as "undifined":

var storage = window.localStorage;
//alert ("cacheKey = " + cacheKey + " ,cacheValue = " + cacheValue);
if(typeof(Storage)!=="undifined"){
localStorage.setItem("cacheKey","cacheValue");
}

Instead, here is the correct way to check if localStorage is available:

if(window.localStorage) { 
localStorage.setItem("cacheKey","cacheValue");
}

With that said, that's not actually causing your problem in this case. Instead, you pointed out that the debugger found an error on this line:

if(k.length < 1) { return; }

You also see the following error:

SCRIPT5007: Unable to get value of the property 'length': object is null or undefined sample_1.html, line 157 character 3

The key piece of information in that error message is that the object is null. In other words, you're passing in a null value as an argument for the parameter k!

The DOMContentLoaded event doesn't pass in this parameter; thus, it may be easiest for you to just simply use a global variable for now, something that you can access from within the restoreContents function.

Also, as @omri pointed out in his answer, +1 BTW, you're passing cacheKey into localStorage as a string and not as the actual variable that represents your key. This is the correct usage:

localStorage.setItem(cacheKey, cacheValue);

This may not be all of the problems in your code, but this should get you started. The best, most useful tip I can suggest for you, since I can tell you're new to this, is to learn how to use those browser debuggers and learn to recognize what error messages mean. Google the error messages if you have to. If you can learn to use these tools, you'll find it becomes much easier to recognize certain problems and then come up with a plan to resolve them. Good luck! :)

Did localStorage just break in Firefox 6.0.2 on Mac?

You should check the storage preferences, type about:config into the location bar. The relevant preferences are:

  • dom.storage.enabled - should obviously be true
  • dom.storage.default_quota - default value is 5120

localStorage doesn't save the data

Different browsers currently treat file: urls differently for localStorage.

This means that if you are just creating a file on your computer and opening it directly, you will have different results depending on the browser you are using.

I believe this was changed in Firefox 8, so if you use 8 or higher it should work for you. Other browsers I am not sure about.

Edit: Is "localStorage" in Firefox only working when the page is online? seems to confirm that FF3-8 will not work how you are doing it.

localStorage Getting NULL?

Are you running the script locally via file:?

If so, Firefox doesn't appear to allow localStorage entries to live beyond unload when file: access is used.

For more info, you may want to look at the question: Is “localStorage” in Firefox only working when the page is online? It's a little dated, but still seems to be applicable.

localStorage.setItem not persisting on refresh

Okay, after a lot of frustration I have the solution. Basically, I was running this locally just from the filesystem as a 'quick' proof of concept. It didn't work in Firefox nor in IE9 but it did work in Chrome.

What I ended up doing was trying this on a real domain, and that seems to have done the trick.

So the conclusion I can draw is that localStorage in Firefox (6.0.2 at least) and IE9 does not work when run on a file-system path. It does in Chrome. Firefox and IE9 require a 'proper' domain to run from, presumably because they are more strict than Chrome in the way they associate the localStorate object to a 'domain' (in Chrome it doesn't need to be a domain as such).

I hope this has helped people as it's frustrated the hell out of me! :)



Related Topics



Leave a reply



Submit