Manu
Manu

Reputation: 10964

localStorage not getting cleared when system shutdown happens

I am using localStorage for sharing data across tabs. I need to clear the localStorage whenever the user closes the tab/browser. I am able to achieve that but the thing is if the user directly shutdowns the system or its a system forced shutdown then the localStorage is not getting cleared. Please suggest any solution for this.

I am using localStorage and not sessionStorage because I want to share data across tabs.

Upvotes: 3

Views: 2645

Answers (3)

T.J. Crowder
T.J. Crowder

Reputation: 1074809

You basically can't rely on getting and processing an event when the user leaves your site. Frequently you'll get an beforeunload, but it's not guaranteed. So you have to code around it.

I need to clear the localStorage whenever the user closes the tab/browser

Sounds like you want to use sessionStorage rather than localStorage. No, sessionStorage doesn't seem to be shared between tabs. Huh, I thought it was.

If for some reason you can't use sessionStorage instead of localStorage, I'd suggest storing some kind of "last used" timestamp in your store, and if it's too old when your page next loads, clearing the data at that point.

Upvotes: 3

Vinay
Vinay

Reputation: 962

sessionStorage, localStorage and Cookies all are used to store data on the client side. Each one has its own storage and expiration limit. sessionStorage: similar to localStorage but expires when the browser closed (not the tab). Cookie: stores data that has to be sent back to the server with subsequent requests.

Upvotes: 1

Ranjith Jayaram
Ranjith Jayaram

Reputation: 299

Session storage will be more help if you want to clear the data.You can place data onto the sessionStorage object and that data persists for as long as that window (or tab) is open.

Upvotes: 0

Related Questions