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

查看:25
本文介绍了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,即使用户从 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天全站免登陆