[英]How to prevent calling onbeforeunload when page refresh?
I am working on React Js Application, I am using Localstorage for managing the Application Token.我正在开发 React Js 应用程序,我正在使用 Localstorage 来管理应用程序令牌。 And i want, when user close the TAB, localsotorage will get empty.
我想,当用户关闭 TAB 时,localsotorage 将变空。 For that i am using beforeunload.
为此,我正在使用 beforeunload。
window.addEventListener("beforeunload", () => localStorage.clear());
But this things also call when i refresh the browser.但是当我刷新浏览器时,这个东西也会调用。 I want to prevent it, that when user refresh the browser, localstorage not clear.
我想防止它,当用户刷新浏览器时,localstorage 不清楚。 How can i achive it?
我怎样才能实现它?
I can not want to use sessionStorage because i need to communicate token/record with other tabs as well.我不想使用sessionStorage ,因为我还需要与其他选项卡通信令牌/记录。
You can use the PerformanceNavigationTiming.type to您可以使用PerformanceNavigationTiming.type来
check if page gets reloaded or refreshed in JavaScript检查页面是否在 JavaScript 中重新加载或刷新
const pageAccessedByReload = (
(window.performance.navigation && window.performance.navigation.type === 1) ||
window.performance
.getEntriesByType('navigation')
.map((nav) => nav.type)
.includes('reload')
);
alert(pageAccessedByReload);
Review Илья Зеленько's original post here在这里查看Илья Зеленько 的原帖
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.