最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Need to clear local storage on browser tab closed not on refresh - Stack Overflow

programmeradmin6浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 4

Refreshing 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();
发布评论

评论列表(0)

  1. 暂无评论