window.onbeforeunload 在 iPad 上不起作用?

发布于 2024-09-09 03:37:16 字数 934 浏览 10 评论 0原文

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

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

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

  • window.onbeforeunload = function(event) { event.returnValue = 'test'; }
  • window.onbeforeunload = function(event) { return 'test'; }
  • (以上两者一起)
  • window.onbeforeunload = function(event) {alert('test')'; }
  • (上述所有功能,但在 内,

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

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

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

分别,结果是:

  • true
  • object
  • null

所以,浏览器确实有属性,但由于某种原因它没有被解雇,

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

有人知道发生了什么吗?我将不胜感激

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

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

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="...">

All of these work on FF and Safari on the PC, but not on the 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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

醉南桥 2024-09-16 03:37:16

这段 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!
    ...
} );

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

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!
    ...
} );
寂寞花火° 2024-09-16 03:37:16

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

更奇怪的是,如果您在 onunload() 中调用了 inform() ,并且用户单击了链接前往其他地方,那么您就可以正常工作了。但是,如果用户关闭 iPad Safari 浏览器选项卡,则会触发 onunload() 事件,但您的 inform() 将会有一个隐式取消作为响应。

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).

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.

庆幸我还是我 2024-09-16 03:37:16

只有苹果公司会确切知道,但我的猜测是,他们故意没有在移动 Safari 中启用该功能,因为它最常被可疑人物用来让你留在他们的网站上或弹出大量色情/广告窗口。

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.

放赐 2024-09-16 03:37:16

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

并且 beforeunload 不在列表中!

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

And the beforeunload is not in the list!

淡淡の花香 2024-09-16 03:37:16

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

相关 Chrome 错误报告

There's a known bug in WebKit with 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.

Related Chrome bug report.

金橙橙 2024-09-16 03:37:16

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

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");
    }
});

移动浏览器往往不支持 beforeunload 因为浏览器可以在不卸载页面的情况下进入后台,然后随时被操作系统杀死时间。

大多数桌面浏览器都包含一个错误,该错误会导致文档卸载时无法调用 visibilityState。请参阅:此处

因此,包含这两个事件以涵盖所有场景非常重要。

NB

在我的示例中,我使用了 console.log 而不是 alert,因为 alert 会被某些浏览器阻止从 beforeunloadvisibilitychange 调用。

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 beforeunload because the browser can go into the background without unloading the page, then be killed by the operating system at any time.

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

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

NB

I have used console.log instead of alert in my example because alert will get blocked by some browsers when called from beforeunload or visibilitychange.

遗忘曾经 2024-09-16 03:37:16

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).

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

烦人精 2024-09-16 03:37:16

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

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 documentation you'll find that it's deprecated and they recommend to use document.pagehide

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文