OGeek|极客世界-中国程序员成长平台

标题: javascript - 未触发 IOS 页面隐藏。无法检测页面卸载 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 22:09
标题: javascript - 未触发 IOS 页面隐藏。无法检测页面卸载

我在页面加载时调用这个函数:

function setBeacon() {
if (!navigator.sendBeacon) {
    alert("No navigator beacon available. This is a critical bug which must be fixed.");
}
else
{
    if ('beforeunload' in window) {
        $(window).on('beforeunload', function() {
            generateFormSendBeacon();
            return undefined;
        });
    } else if ('onpagehide' in window) {
        alert("on page hide");
        window.addEventListener("pagehide", function() {
            generateFormSendBeacon();
            return undefined;
        });
    } else {
        alert("Neither beforeunload or onpagehide events are available. Please report this to John, with details about your browser.");
    }
}

这在桌面上完美运行。我在 generateFormSendBeacon() 函数中使用 navigator.sendBeacon。它正确发送它(我的服务器中有一个断点接收它)。然而,在移动设备上,在 safari 和 chrome 中,“beforeunload”事件不存在。 But according to apple, we should use pagehide instead. “页面隐藏”警报显示; iOS 确实有权访问此事件。但是,该事件从未被触发,因为我从未到达服务器中的断点。再次;在桌面上测试相同的代码路径,它工作正常。如何检测 iOS 上的页面卸载?



Best Answer-推荐答案


一些浏览器ignore dialog prompts when unloading the page .这是意料之中的。

您可能遇到了WebKit issue #193508 .

关于javascript - 未触发 IOS 页面隐藏。无法检测页面卸载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54703437/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4