admin管理员组文章数量:1423156
I'm trying to handle some logic on before unload and I don't want that logic to run if the user is reloading the page or going back.
I've set up something like this.
window.onbeforeunload = function(e) {
if(window.event && window.event.clientX){ //IE
//some logic
} else if (e.currentTarget.performance.navigation.type === e.currentTarget.performance.navigation.TYPE_RELOAD) {
// another logic
} else if(e.currentTarget.performance.navigation.type === e.currentTarget.performance.navigation.TYPE_BACK_FORWARD){
// yet another logic
}
}
I have other code to handle refresh and such from keyboard input that all seems to be working ok. Right now I'm concerned with this piece of code. For some reason on the first reload or back button the navigation.type es back as 0, but after that all other reloads or back buttons populate the correct value in navigation.type. Even in IE on the first reload something is not being set correctly (not sure if its the mouse location or what yet). What could be causing something like this?
I'm trying to handle some logic on before unload and I don't want that logic to run if the user is reloading the page or going back.
I've set up something like this.
window.onbeforeunload = function(e) {
if(window.event && window.event.clientX){ //IE
//some logic
} else if (e.currentTarget.performance.navigation.type === e.currentTarget.performance.navigation.TYPE_RELOAD) {
// another logic
} else if(e.currentTarget.performance.navigation.type === e.currentTarget.performance.navigation.TYPE_BACK_FORWARD){
// yet another logic
}
}
I have other code to handle refresh and such from keyboard input that all seems to be working ok. Right now I'm concerned with this piece of code. For some reason on the first reload or back button the navigation.type es back as 0, but after that all other reloads or back buttons populate the correct value in navigation.type. Even in IE on the first reload something is not being set correctly (not sure if its the mouse location or what yet). What could be causing something like this?
Share Improve this question edited Feb 17, 2016 at 23:20 Kizer 1,6662 gold badges17 silver badges24 bronze badges asked Feb 17, 2016 at 21:38 I can be anythingI can be anything 1844 silver badges13 bronze badges 2- Even while closing a browser's tab, performance.navigation.type gives you 1 (performance.navigation.TYPE_RELOAD)! Can we actually detect the browser is about to refresh? – Aniruddha Shevle Commented Jun 25, 2020 at 8:32
- can you detect STOP RELOAD ? cross button after reload? – Igor Romanchuk Commented Nov 10, 2021 at 13:01
1 Answer
Reset to default 4First of all, I think what you wanted to write was e.currentTarget.performance.navigation.type
(not e.current.performance.navigation.type
), which is the same as writing window.performance.navigation.type
. This variable tells you how this page was navigated to, not the type of navigation that the page is exiting through.
Why you get performance.navigation.type
as 0
(performance.navigation.TYPE_NAVIGATE
) the first time is that the page was loaded by direct navigation that first time. Subsequent reloads will set performance.navigation.type
to 1 (performance.navigation.TYPE_RELOAD
) because the page is now loaded by reloading. So, you are getting the method that was used to load the page, not the method that the user is using to exit the page.
本文标签: javascriptUnexpected performacenavigationtype onbeforeunloadStack Overflow
版权声明:本文标题:javascript - Unexpected performace.navigation.type onbeforeunload - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745365673a2655500.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论