1

I made a small JavaScript math game for my kids that stores a high score using localStorage. It works fine, does what it's supposed to do, and saves the high score even when the browser is closed or computer is turned off. However, today I tried to play the game for testing over a different Internet connection, and the two localStorage items I used (one for the score, one for the name) both returned undefined.

Both times the game was played in Chrome v55 in Windows 7 (same PC). Originally it was over our home Wi-Fi connection, but today it was over a public (coffee shop) Wi-Fi connection.

localStorage still works over the public Wi-Fi connection -- I can set new values and retrieve them -- but I'm wondering why it didn't store the original values. No browser history or cookies have been cleared. Does anyone know if localStorage has separate containers for different Internet connections?

UPDATE: The localStorage object works fine -- it saves and persists data -- the only problem is that when retrieved when connected to a different network connection, it loses the values.

This is the code used to get data from localStorage:

if (localStorage.mb_hiscore == 0) { localStorage.mb_name = " "; } hiscore.innerHTML = localStorage.mb_hiscore + "<br />" + localStorage.mb_name;

Update again: Chrome shows the same origin on both the public Wi-Fi and my home Wi-Fi:

origin setting, file

Also, the localStorage values I set while on the public Wi-Fi connection persisted to when I retrieved them later, on my home network, so it's only the one time so far that Chrome dropped the values.

freginold
  • 3,946
  • 3
  • 13
  • 28
  • 2
    No, it does not. I have to ask - where you using the same PC both times? – Reinstate Monica Cellio Dec 19 '16 at 16:58
  • 1
    How are you accessing the webapp? Is it a local app (i.e. `localhost`) or over the network? If over the network, did you happen to switch IPs/domains to access the app? – Joseph Dec 19 '16 at 16:59
  • is it possible you had a different browser session? EG private/incognito mode? – Vinny Dec 19 '16 at 17:00
  • @Archer - yup, same PC both times – freginold Dec 19 '16 at 17:01
  • @Vinny - no, wasn't using incognito mode either time – freginold Dec 19 '16 at 17:01
  • @JosephtheDreamer - it was a local app (file:/// protocol), the same protocol and file both times. – freginold Dec 19 '16 at 17:02
  • Possible duplicate of [Javascript/HTML Storage Options Under File Protocol (file://)](http://stackoverflow.com/questions/5914029/javascript-html-storage-options-under-file-protocol-file) – Heretic Monkey Dec 19 '16 at 17:06
  • did the public wifi have a proxy of some kind? – Vinny Dec 19 '16 at 17:06
  • @MikeMcCaughan - that question is different, it deals with `localStorage` not working at all with the file protocol. It worked fine for me, just didn't seem to persist across a different connection. – freginold Dec 19 '16 at 17:16
  • If you're using the `file:` protocol, there's no network connection being used. – Barmar Dec 19 '16 at 17:18
  • @freginold Whatever the reason, it was not because you had a different internet connection. Go back to your original internet connection and the original data will still be gone. – Reinstate Monica Cellio Dec 19 '16 at 17:19
  • @Barmar that's why I thought it was weird – freginold Dec 19 '16 at 17:20
  • @Archer gonna try that when I get home tonight, see what happens – freginold Dec 19 '16 at 17:20
  • @Vinny I believe it did use a proxy but I'm not that familiar with them and I'm not there any more – freginold Dec 19 '16 at 17:21
  • Can you post a [MCVE](http://stackoverflow.com/help/mcve) that demonstrates the problem? – Barmar Dec 19 '16 at 17:21
  • @Barmar there's really not much code to show; I'll update the question to include it – freginold Dec 19 '16 at 17:23
  • Seeing your update, if it happened only one time so far its probably some details that you've missed. Unless you come up with a way to reproduce your problem there's isn't much we can do. – cvsguimaraes Dec 21 '16 at 12:27
  • @snolflake it looks to me like maybe a one-off. I've since connected to both networks (home and the public Wi-Fi) and the `localStorage` values persisted. Thanks for the info about origins, now I know more than I did before. – freginold Dec 21 '16 at 15:38

1 Answers1

2

Each origin has its very own localStorage. If you check the value of document.location.origin you may get something like this for example: "http://localhost:8080"

Depending on how you're accessing your app, if any of these parts change, be it the hostname, the port number or the protocol, you'll be dealing if a different origin therefore modifying its own storage. From the browser perspective is a completely different end point.

Try to reproduce your issue again, this time paying attention to the address you're using.

cvsguimaraes
  • 12,910
  • 9
  • 49
  • 73
  • thanks, I'll check that when I get home. checking the origin when I loaded the game while connected to the public Wi-Fi yielded "file:///" so I would imagine it would be the same over the other connection, since it's a local file, but I'll check just to be sure. – freginold Dec 19 '16 at 19:03
  • I checked the origin on my home network and got the same thing. `localStorage` persisted from the public Wi-Fi to my home network. – freginold Dec 19 '16 at 23:44