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