火灾在卸载前确认警报仅适用于外部站点

Fire onbeforeunload confirm alert for external sites only

本文关键字:适用于 外部 站点 卸载 确认 火灾      更新时间:2023-09-26

如果用户离开网站,我使用window.onbeforeunload方法显示确认消息。

window.onbeforeunload = function(e) {
    return textMsg;
};

但只有当用户导航到外部网站或关闭窗口时,我才需要触发它。

那么,如何为同一域内的所有XHR取消此确认消息以及表单提交等?

Try(未经测试的代码):

window.onbeforeunload = function(e) {
    return textMsg;
};
$('a:not([href^="http"])').on('click', function() {
    window.onbeforeunload = null; // prevent message
});
$('form').on('submit', function() {
    window.onbeforeunload = null; // prevent message
});

如果链接不是以http(外部链接)开头,并且对于<form>提交,这将防止触发事件。目前正在寻找关闭窗口的解决方案。