卸载前出现意外的performace.navigation.type [英] Unexpected performace.navigation.type onbeforeunload

查看:193
本文介绍了卸载前出现意外的performace.navigation.type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在卸载之前处理一些逻辑,并且如果用户正在重新加载页面或返回页面,我不希望该逻辑运行。

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

我还有其他代码可以处理刷新,例如键盘输入似乎一切正常。现在,我正在关注这段代码。由于某些原因,在第一个重新加载或后退按钮上,navigation.type返回为0,但在此之后,所有其他重新加载或后退按钮将在Navigation.type中填充正确的值。即使在IE上第一次重新加载,也无法正确设置某些内容(不确定它的鼠标位置还是什么)。

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 comes 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?

推荐答案

首先,我想您想写的是 e.currentTarget.performance.navigation.type (不是 e.current.performance.navigation.type ),与编写 window.performance.navigation.type 。此变量告诉您​​该页面是如何导航到的,而不是页面正在通过的导航类型。

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.

为什么获得 performance.navigation.type 0 performance.navigation.TYPE_NAVIGATE ),这是第一次通过直接导航加载 页面。后续重新加载会将 performance.navigation.type 设置为1( performance.navigation.TYPE_RELOAD ),因为页面现在通过 重新加载进行加载。因此,您将获得用于加载页面的方法,而不是用户用于退出页面的方法。

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.

这篇关于卸载前出现意外的performace.navigation.type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆