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
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
}