最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Unexpected performace.navigation.type onbeforeunload - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 4

First 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.

发布评论

评论列表(0)

  1. 暂无评论