[英]How to check if user is refreshing the page or navigating to other page
場景:每當用戶離開頁面時,我都想在本地存儲中設置一些變量。
使用window.onbeforeunload
我得到用戶即將離開頁面的事件。 但是我想在用戶刷新時設置不同的變量值,並在頁面導航上設置不同的值
var someVar;
//when refresh someVar = 1;
//When user clicks on any link which is not the same page, someVar =2
是否可以檢測用戶是否正在刷新或即將離開頁面?
根據您的評論,我正在更新我的答案。 您無法在beforeunload
事件或任何其他方法上檢測導航類型,因為它取決於瀏覽器。 但我可以為您提供一種在頁面加載后進行回退或刷新的方法。 我希望它會有用。
請檢查效果導航
if (performance.navigation.type == PerformanceNavigation.TYPE_RELOAD){
//Reload
}
if (performance.navigation.type == PerformanceNavigation.TYPE_BACK_FORWARD){
//Back Button
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.