How to check if user is refreshing the page or navigating to other page

前端 未结 1 1542
深忆病人
深忆病人 2021-01-28 03:45

Scenario: I want to set some variable in my localstorage whenever user leaves the page.

Using window.onbeforeunload I get the event that user is about to le

相关标签:
1条回答
  • 2021-01-28 04:29

    Based on your comment I am updating my answer. You can't detect navigation type on beforeunload event or with any other method because it is browser dependent. But I can provide you a way to detect back or refresh after the page loaded. I hope it will be useful.

    Please check performance navigation

    if (performance.navigation.type == PerformanceNavigation.TYPE_RELOAD){
     //Reload
    }
    
    if (performance.navigation.type == PerformanceNavigation.TYPE_BACK_FORWARD){
     //Back Button
    }
    
    0 讨论(0)
提交回复
热议问题