Need to clear local storage on browser tab closed not on refresh. I tried
window.onbeforeunload = function (e) {
localStorage.clear();
};
it clears the localstorage when browser refresh happens. I want to force the user to login when the user close browser tab.
Need to clear local storage on browser tab closed not on refresh. I tried
window.onbeforeunload = function (e) {
localStorage.clear();
};
it clears the localstorage when browser refresh happens. I want to force the user to login when the user close browser tab.
Share Improve this question edited Oct 16, 2017 at 12:12 phuzi 13.1k4 gold badges29 silver badges59 bronze badges asked Oct 16, 2017 at 12:09 SankarSankar 3592 gold badges6 silver badges20 bronze badges 4- 1 Possible duplicate of stackoverflow./questions/9943220/… – Karan Singh Dhir Commented Oct 16, 2017 at 12:39
-
Try this
localStorage.$reset();
– Ramesh Rajendran Commented Oct 16, 2017 at 12:45 - 1 Possible duplicate of How to delete a localStorage item when the browser window/tab is closed? – Ramesh Rajendran Commented Oct 16, 2017 at 12:45
- stackoverflow./questions/39128931/… – Shub Commented Mar 29, 2024 at 14:30
2 Answers
Reset to default 4Refreshing and closing a tab/window is the same event as far as the browsers are concerned.
Best would be to use session storage
rather than local storage
as session would be active until the tab/window is closed and not refreshed and you aren't saving persistent data.
Have a look here
Use this code.
window.onload = window.localStorage.clear();