Javascript window.onbeforeunload 不适用于 iPad?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3239834/
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 03:48:52  来源:igfitidea点击:

window.onbeforeunload not working on the iPad?

javascriptiosipadonbeforeunload

提问by Art Zambrano

Does anyone know if the onbeforeunloadevent is supported on the iPad and/or if there's a different way to use it?

有谁知道onbeforeunloadiPad 是否支持该事件和/或是否有不同的使用方式?

I've tried pretty much everything, and it seems like the onbeforeunloadevent is never triggered on the iPad (Safari browser).

我几乎尝试了所有方法,似乎onbeforeunload从未在 iPad(Safari 浏览器)上触发该事件。

Specifically, this is what I've tried:

具体来说,这是我尝试过的:

  • window.onbeforeunload = function(event) { event.returnValue = 'test'; }
  • window.onbeforeunload = function(event) { return 'test'; }
  • (both of the above together)
  • window.onbeforeunload = function(event) { alert('test')'; }
  • (all of the above functions but inside <body onbeforeunload="...">
  • window.onbeforeunload = function(event) { event.returnValue = 'test'; }
  • window.onbeforeunload = function(event) { return 'test'; }
  • (以上两个一起)
  • window.onbeforeunload = function(event) { alert('test')'; }
  • (上述所有功能,但在内部 <body onbeforeunload="...">

All of these work on FF and Safari on the PC, but not on the iPad.

所有这些都适用于 PC 上的 FF 和 Safari,但不适用于 iPad。

Also, I've done the following just after loading the page:

另外,我在加载页面后完成了以下操作:

alert('onbeforeunload' in window);
alert(typeof window.onbeforeunload);
alert(window.onbeforeunload);

Respectively, the results are:

结果分别为:

  • true
  • object
  • null
  • true
  • object
  • null

So, the browser does have the property, but for some reason it doesn't get fired.

因此,浏览器确实具有该属性,但由于某种原因它没有被触发。

The ways I try to navigate away from the page are by clicking the back and forward buttons, by doing a google search in the top bar, by changing location in the address bar, and by clicking on a bookmark.

我尝试离开页面的方法是单击后退和前进按钮、在顶部栏中进行谷歌搜索、更改地址栏中的位置以及单击书签。

Does anyone have any idea about what's going on? I'd greatly appreciate any input.

有没有人知道发生了什么?我将不胜感激任何输入。

Thanks

谢谢

回答by Danny Armstrong

I have found that the onunload() event does fire. It's behavior is somewhat odd; whatever you have in your callback function attached to the event is actually run after the new page has loaded in the background (You can't tell it's loaded yet, but server logging will show that it has).

我发现 onunload() 事件确实触发了。它的行为有点奇怪;附加到事件的回调函数中的任何内容实际上是在新页面在后台加载后运行的(您无法判断它是否已加载,但服务器日志会显示它已加载)。

More oddly, if you have a confirm() call in your onunload(), and the user has clicked a link to go somewhere else, you are in business. If, however, the user closes the iPad Safari browser tab, the onunload() event will fire, but your confirm() will have an implicit cancel as response.

更奇怪的是,如果您在 onunload() 中有一个 confirm() 调用,并且用户点击了一个链接去其他地方,那么您就在做生意。但是,如果用户关闭 iPad Safari 浏览器选项卡,则 onunload() 事件将触发,但您的 confirm() 将具有隐式取消作为响应。

回答by Danger

This bit of JavaScript works for me on Safari and Chrome on ipad and iphone, as well as desktop/laptop/other browsers:

这段 JavaScript 代码适用于 ipad 和 iphone 上的 Safari 和 Chrome,以及台式机/笔记本电脑/其他浏览器:

var isOnIOS = navigator.userAgent.match(/iPad/i)|| navigator.userAgent.match(/iPhone/i);
var eventName = isOnIOS ? "pagehide" : "beforeunload";

window.addEventListener(eventName, function (event) { 
    window.event.cancelBubble = true; // Don't know if this works on iOS but it might!
    ...
} );

回答by Charles Boyung

Only Apple would know for sure, but my guess is that they purposely did not enable that functionality in mobile Safari because it is most often used by shady characters to get you to stay on their site or pop up lots of porn/advertising windows.

只有 Apple 会确切知道,但我的猜测是他们故意没有在移动 Safari 中启用该功能,因为它最常被阴暗的角色用来让您留在他们的网站上或弹出大量/广告窗口。

回答by Joel Mueller

There's a known bug in WebKitwith onbeforeunload. I believe it's fixed in the latest beta of Chrome 5, but it's quite possible the iPad's browser is made from a version of WebKit that doesn't have the fix.

WebKit 中存在一个带有 onbeforeunload的已知错误。我相信它在 Chrome 5 的最新测试版中得到了修复,但 iPad 的浏览器很可能是由没有修复的 WebKit 版本制成的。

Related Chrome bug report.

相关的 Chrome 错误报告

回答by Julien Bachmann

beforeunload event is not supported by Mobile Safari. You can see the list of all supported events here: Handling Events Apple documentation

Mobile Safari 不支持 beforeunload 事件。您可以在此处查看所有支持事件的列表:处理事件 Apple 文档

And the beforeunload is not in the list!

而且 beforeunload 不在列表中!

回答by zelda.j

https://code.google.com/p/chromium/issues/detail?id=97035

https://code.google.com/p/chromium/issues/detail?id=97035

see hear.

看到听到。

alerts are no longer allowed during page dismissal events (beforeunload, unload, pagehide).

在页面关闭事件(beforeunload、unload、pagehide)期间不再允许警报。

I think alerts, prompt, confirm, and other actions like these are also no longer allowed.

我认为也不再允许警报、提示、确认和其他类似的操作。

回答by Miquel

If you just need to know if the page has been left you can use document.unload. It works fine in ios browsers. If you see on Apple documentationyou'll find that it's deprecated and they recommend to use document.pagehide

如果您只需要知道页面是否已离开,您可以使用document.unload. 它在 ios 浏览器中运行良好。如果您在Apple 文档中看到,您会发现它已被弃用,他们建议使用 document.pagehide

回答by Dan Bray

Here's a solution that should work on all modern browsers:

这是一个适用于所有现代浏览器的解决方案:

var unloaded = false;
window.addEventListener("beforeunload", function(e)
{
    if (unloaded)
        return;
    unloaded = true;
    console.log("beforeUnload");
});
window.addEventListener("visibilitychange", function(e)
{
    if (document.visibilityState == 'hidden')
    {
        if (unloaded)
            return;
        unloaded = true;
        console.log("beforeUnload");
    }
});

Mobile browsers don't tend to not support beforeunloadbecause the browser can go into the background without unloading the page, then be killed by the operating system at any time.

手机浏览器往往不支持,beforeunload因为浏览器可以不卸载页面就进入后台,然后随时被操作系统杀死。

Most desktop browser contain a bug that causes visibilityStateto not get called when the document unloads. See: here.

大多数桌面浏览器都包含一个错误,导致visibilityState在文档卸载时无法调用。见:这里

Therefore, it's important to include both events to cover all scenarios.

因此,重要的是要包括这两个事件以涵盖所有场景。

NB

NB

I have used console.loginstead of alertin my example because alertwill get blocked by some browsers when called from beforeunloador visibilitychange.

我在我的例子中使用了console.log而不是alert因为alert当从beforeunload或调用时会被某些浏览器阻止visibilitychange