onbeforeunload和onunload之间的区别 [英] Difference between onbeforeunload and onunload

查看:204
本文介绍了onbeforeunload和onunload之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

onbeforeunload和onunload有什么区别?
另外我还有一个与iPad相关的具体问题......我有一个页面(myPage.html),我试图在页面关闭时显示警告(即按下X关闭iPad上的标签)

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)

现在我尝试使用window.onunload和window.onbeforeunload
以下是我在iPad上的调查结果;

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


  1. 使用window.onunload,我可以在用户导航到myPage.html的其他页面时收到提醒(通过点击某个链接或进行操作)在myPage.html上进行Google搜索)。但是,从最小化视图(X)关闭选项卡时没有任何反应

  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)

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

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.

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

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

谢谢。

推荐答案

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

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

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

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

我知道Opera曾经不承认 onbeforeunload - 不确定他们是否已修复此问题,但我总是注册监听器以保证两者的安全:

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:

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

这篇关于onbeforeunload和onunload之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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