Safari未触发window.pagehide,window.unload

时间:2018-07-23 23:34:00

标签: javascript safari window.onunload

我已经在stackoverflow上找到了相同的问题,但是没有一个解决方案有帮助(更不用说许多已过时的解决方案)。我的问题是Safari忽略了window.unload和window.pagehide事件。但是,它们确实响应window.beforeunload事件。

我的代码打开一个新窗口,然后处理其“关闭”事件:

//create new window
var windowReference = null;
if (windowReference == null || windowReference.closed) {
    ...set new window's dimensions here...
    windowReference = window.open("MyHTMLFile.html", "_blank",
    "toolbar=no ... left=" + left + ", top=" + top);

    window.addEventListener("message", receiveWebChatClosedMessage, false)
}

//monitor for the closed event
function receiveWebChatClosedMessage() {
    window.setTimeout(function () {
        if (windowReference.closed) {
            ...trivial operations...
        }
    }, 1);
}

,这是Safari忽略的window.unload侦听器:

if ('onpagehide' in window) {
    window.addEventListener('pagehide', myUnload, false);
} else {
    window.addEventListener('unload', myUnload, false);
}

//doesn't get called in Safari
function myUnload() {
    var win = window.opener;
    if (!win.closed) {
        win.postMessage("Window Closed", location.protocol + "//" + location.host);
    }
}

谢谢!

0 个答案:

没有答案