views:

1239

answers:

4

So i'm toying around with HTML 5 and the localStorage and i'm noticing that my values only get stored when i run the page in Firefox on the local host (i.e. http://127.0.0.1:8000/test/index.html) when i run the file locally (file:///C:/test/index.html) my values won't get stored. Safari 4 has no problems with both setups.
So does anybody know if this is by design -> DOM Storage on the Mozilla Developer Center

(Firefox 2 permitted access to storage objects higher in the domain hierarchy than the current document. This is no longer allowed in Firefox 3, for security reasons. In addition, this proposed addition to HTML 5 has been removed from the HTML 5 specification in favor of localStorage, which is implemented in Firefox 3.5.)

Or if there is a workaround?
I wonder because offline storage that works only online sounds silly :P

If anybody wonders, code is as easy as it gets:

function save()
{
 localStorage.setItem('foo','bar');
}

function load()
{
 var test = localStorage.getItem('foo');
 alert(test);
}
+8  A: 

Well, the linked document does say that

localStorage is the same as globalStorage[location.hostname], with the exception of being scoped to an HTML5 origin (scheme + hostname + non-standard port)

I don't want to claim that I understand 100% what that means, but the bit in brackets would suggest that the URL needs to have certain properties - in particular that the scheme and hostname are what Firefox considers an HTML 5 origin. I suspect that file:/// URLs don't match this, while your http://127.0.0.1/ does.

edit: Looking at the W3C's description of the Origin property, step 7 looks like it might be causing the problem. Depending on how the localStorage handling is implemented, it may be expecting a 3-tuple as returned by step 12, but for a file:// URL the return value may be just about anything.

So, er, I suppose it is by design. On reflection, chances are that this isn't really by design; there's no reason why localStorage shouldn't work for file:// URLs. It might just be a case of the output of one browser-specific implementation not matching the expectations of another.

As for workarounds, would globalStorage not do what you want here?

Andrzej Doyle
+4  A: 

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

In the meantime you can use Chrome to offline testyour scripts, as it doesn't have this bug.
lapo
A: 

...but seems working well offline with chrome

lapinferoce
A: 

Thank god I found this...I've been trying to debug in Firefox and was wondering where I went wrong because I couldn't see anything wrong with my code. It worked in IE and Chrome while working with file:// URL but Firefox wasn't working. As soon as I put it on a server, it worked just fine.

Vedran