I have a website which saves a stringified JavaScript object into the sessionStorage. It works well on desktop browsers - data survives over page reloads as per described on MDN.
On Android phones, it is not uncommon that a tab is reloaded when user switches back from another tab or when the browser is brought back from background to foreground, and I expect the sessionStorage will persist. This doesn't however seem to be the case. The sessionStorage is gone missing, as if a new session is created.
I have thought of using localStorage instead, which will not expire even if the session is closed. It doesn't really sort out all my problems though - as I expect a user can open it multiple times on multiple tabs, and do different things on each of them.
So my questions are:
- Is the sessionStorage behavior described above as expected? If not, what can I do to rectify it?
- If the behavior is as expected, and I have to rely on localStorage, how can I identify the object stored for each individual tab?
Thanks in advance for your help!
unbeforeunload
event to save data to the sessionStorage. This work on mobile (but maybe to all) when you close the page or go to another page. But when the user goes to another tab and does not open the webbrowser tab for a while the page will be reloaded when the tab is reopened, but thebeforeunload
is never fired. I am now using:pagehide
andunload
as well to set the session. It seems to be working for now. – Lightsome