I have to clear local storage when my website is closed from all the tabs or browser windows is closed by clicking close button or any short cut key, I have Google for it since 2-3 days, but not getting any success. Do you have any way to achieve this?
Asked
Active
Viewed 1.0k times
4
-
clear the storage during the "unload" event. – Christoph May 20 '14 at 12:18
4 Answers
0
You can use the onunload
event in your body
tag to trigger local storage clearing:
<body onunload = "javascript:window.localStorage.clear()">
There is one caveat, however, the onunload
event is supported in IE, Firefox, and Safari, but not supported properly in Chrome or Opera yet.

Bud Damyanov
- 30,171
- 6
- 44
- 52
-
where do you have the information, that chrome does not support the `unload` event? – Christoph May 20 '14 at 12:23
-
Read carefully - "properly", means that older versions of Chrome have problems with it, http://stackoverflow.com/questions/12182559/i-cant-trigger-the-unload-event-in-chrome, and also https://www.webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/ – Bud Damyanov May 20 '14 at 12:27
-
hi @bodi0 and christoph, but If user press ctrl+f5 or refresh button, I want local storage remain same, If 2 tabs has my websites open, and some close just one tab, then also local storage remain, when all tabs are closed from browser, then only I have to remove local storage. – Manish Sapkal May 20 '14 at 12:28
-
You will have to combine this with cross tab messaging: http://stackoverflow.com/a/1100416/1816580 – Artjom B. May 20 '14 at 12:32
-
@ArtjomB. no need for complicated messaging, sessionStorage will do fine. – Christoph May 20 '14 at 14:37
-
0
Instead of localStorage
, you have to use the lesser known little brother sessionStorage
. It does exactly what you want - store the data until the end of the session.
Check out this example. Reload it a couple of times to see the counter increment. After closing the window, the counter will get reset.

Christoph
- 50,121
- 21
- 99
- 128
0
You can use sessionStorage : if you want to handle in another tabs. localStorage : just in current tabs.
window.onbeforeunload = function(){
window.localStorage.clear();
window.sessionStorage.clear();
}
Hope it'll help.

LogPi
- 706
- 6
- 11
-
1clearing the sessionStorage in the unload Event will break the functionality, as a normal refresh also triggers this event. – Christoph May 20 '14 at 15:04
-
It will be the same with onbeforeunload event? Thanks for your informations about the break of "unload" . Very useful. – LogPi May 20 '14 at 15:08
-4
window.localStorage.removeItem(key);

tmthydvnprt
- 10,398
- 8
- 52
- 72

fgfgfg
- 1
-
Code without explanation is not helpful. Here you must *at least* explain under what circumstances this code should be invoked. Moreover, the code you have does not actually *clear* the storage: it removes only one key from the storage. I'll add before you start editing this that if you edit it in such a way that it presents a solution that has already been presented, the you should just delete it. Duplicate solutions are not welcome. – Louis Aug 16 '16 at 11:11