Question

OK, bear with me please, I'll try to explain the issue as detailed as possible.

I have 3 HTML (and appropriate .js) files. I can add stuff to sessionStorage and I display the content of the sessionStorage when I'm located in either of the HTML files. I'm running files locally on the latest version of firefox, which - as far as I know - supports sessionStorage in file:// So, everything works great, I can add stuff to sessionStorage in HTML1 and go to HTML2 and the data is displayed properly. I get the concept of sessionStorage and all is good.

But here's where it gets weird: when I add stuff to sessionStorage in HTML1 and go to HTML2 via a-href, the content of sessionStorage is there. But if I click in the url bar and press enter, the content is lost. SessionStorage is "null". I go back to HTML1 and press ctrl+r (reload) and still, null. But when I click in url bar and press enter (while I'm in HTML1 page), sessionStorage content returns.

I've done several checks and it appears as if when I click in url bar and press enter, another sessionStorage instance is created.

This, however, does NOT happen if I upload the project on some server. I'm wondering why - if firefox supports sessionStorage - this is happening when I run the application locally and what is really going on there? Thanks for any kind of explanation.

Was it helpful?

Solution

Ok, it doesn't work locally since you are opening the files, you need to run it on a server.

The browser will tie the sessions together using the URL (and other internal stuff of course) which is why it works on the whatever server you upload to. This won't work with the file URLs as the browser seems them as different pages from different locations, and so the session isn't shared.

If you set up WAMP or something locally and run it through that, it will work. Which is exactly what xavierm02 suggested.

OTHER TIPS

I'm quite sure that if you looks at files from the filesystem, browsers consider every single file like a domain. So no Ajax, no shared cookies, sessions etc. The thing I find weird is that you get to keep some sessions...

Imagine you have a user stupid enough to open an html file he downloaded, without those protections, that html file could see everything on the system. And as much as antiviruses and the OS warn you when you open a .exe (or equivalent), you won't get any kind of warning when opening a .html file.

You'll have to use a local server, something like WAMP probably. Or Apache if you can.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top