Javascript onbeforeunload 和 onunload 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6895564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:39:29  来源:igfitidea点击:

Difference between onbeforeunload and onunload

javascriptipadsafarimobile-safarionbeforeunload

提问by testndtv

What are the differences between onbeforeunload and onunload ? Also I have a specific question related to it's use on the iPad...I have a page (myPage.html) where I am trying to show an alert when the page is closed (i.e. X is pressed to close the tab on iPad)

onbeforeunload 和 onunload 有什么区别?另外我有一个与它在 iPad 上使用相关的特定问题......我有一个页面 (myPage.html),我试图在页面关闭时显示警报(即按下 X 以关闭 iPad 上的选项卡)

Now I tried using both window.onunload and window.onbeforeunload Below are my findings on the iPad;

现在我尝试同时使用 window.onunload 和 window.onbeforeunload 下面是我在 iPad 上的发现;

  1. Using window.onunload , I am able to get an alert when user navigates to a different page from myPage.html (either by clicking on some link or doing a Google search while on myPage.html) . However nothing happens when the tab is closed from the minimized view (X)

  2. Using window.onbeforeunload, I neither get an alert even if the user navigates to a different page from myPage.html OR if he closes the tab (X) from the minimized view.

  1. 使用 window.onunload ,当用户从 myPage.html 导航到不同的页面时(通过单击某个链接或在 myPage.html 上进行 Google 搜索),我能够收到警报。但是,从最小化视图 (X) 关闭选项卡时没有任何反应

  2. 使用 window.onbeforeunload,即使用户从 myPage.html 导航到不同的页面,或者他从最小化视图中关闭选项卡 (X),我也不会收到警报。

I wanted to know if there is any alternate way to fix this issue ?

我想知道是否有其他方法可以解决此问题?

Thank you.

谢谢你。

采纳答案by AlienWebguy

onunloadis responsible for executing an instruction when the page is closed. It also causes issue with IE and AJAX.

onunload负责在页面关闭时执行一条指令。它还会导致 IE 和 AJAX 出现问题。

onbeforeunloadis more efficient because it does not run in competition with the actual closing of the window and is triggered before onunload

onbeforeunload效率更高,因为它不会与窗口的实际关闭竞争运行,并且会在之前触发 onunload

I know Opera used to not acknowledge onbeforeunload- not sure if they've fixed that, but I always register the listener for both to be safe:

我知道 Opera 过去不承认onbeforeunload- 不确定他们是否已经解决了这个问题,但我总是为两者注册监听器以确保安全:

window.onunload = window.onbeforeunload = (function(){...

回答by noboundaries

Adding with AlienWebguy's ans, to avoid dual calls at the browsers that support both events,

添加 AlienWebguy 的 ans,以避免在支持这两个事件的浏览器上进行双重调用,

var onBeforeUnLoadEvent = false;

window.onunload = window.onbeforeunload= function(){
if(!onBeforeUnLoadEvent){
  onBeforeUnLoadEvent = true;
  //your code here
  }
};

回答by B T

onbeforeunload:

onbeforeunload

  • Called before unloading begins
  • MDN tells meyou can cancel the closing of the page using event.preventDefault();
  • or by returning a non-void value (ie a value != 0), the page will pop up a confirmation dialog that allows the user to choose to cancel the close
  • MDN also says Opera 12 and up support onbeforeunload - looks like its supported it for a while now
  • 在卸载开始之前调用
  • MDN 告诉我你可以使用取消关闭页面event.preventDefault();
  • 或者通过返回一个非空值(即一个值!= 0),页面会弹出一个确认对话框,允许用户选择取消关闭
  • MDN 还说 Opera 12 及更高版本支持 onbeforeunload - 看起来它现在支持它一段时间了

onunload:

onunload

  • Called after unloading has begun, but before any resource deallocation (its not clear to me what exactly isdone during this period)
  • Its too late to cancel the page close at this point
  • 卸载开始后调用,但任何资源释放之前(它不是很清楚,我究竟在此期间完成)
  • 此时取消页面关闭为时已晚

The only reason I can think of why you would want to use onunloadover onbeforeunloadwould be where your onunloadcode could take some significant time, and don't want the user to see a window that hangs while closing.

我能想到为什么要使用onunloadover的唯一原因onbeforeunload是您的onunload代码可能需要花费大量时间,并且不希望用户看到关闭时挂起的窗口。

回答by dvdplm

One significant difference (other than cancelability) between the onbeforeunloadand onunloadis that the former is triggered for download links and the latter is not. Example: <a href="https://somewhere.com/thething.zip">download</a>will trigger the onbeforeunloadhandler, but not the onunload.

onbeforeunload和之间的一个显着区别(可取消性除外)onunload是前者是针对下载链接触发的,而后者则不是。示例:<a href="https://somewhere.com/thething.zip">download</a>将触发onbeforeunload处理程序,但不会触发onunload.