场景:每当用户离开页面时,我想在我的本地存储中设置一些变量。
使用window.onbeforeunload,我得到了用户将要离开页面的事件。但是我想在用户刷新时设置不同的变量值,在页面导航上设置不同的变量值
var someVar;
//when refresh someVar = 1;
//When user clicks on any link which is not the same page, someVar =2是否可以检测用户是否正在刷新或即将离开页面?
发布于 2017-07-31 18:08:22
根据你的评论,我正在更新我的答案。您不能通过beforeunload事件或任何其他方法检测导航类型,因为它依赖于浏览器。但我可以为您提供一种在页面加载后检测返回或刷新的方法。我希望这将是有用的。
if (performance.navigation.type == PerformanceNavigation.TYPE_RELOAD){
//Reload
}
if (performance.navigation.type == PerformanceNavigation.TYPE_BACK_FORWARD){
//Back Button
}https://stackoverflow.com/questions/45412931
复制相似问题