Html - beforeunload - 跨浏览器

时间:2017-03-17 18:58:37

标签: html onbeforeunload

编辑:这个问题不同,因为我要求在2017浏览器中提供解决方案。另一个问题涉及几年前的浏览器。它也没有解决方案。必须有某种解决方案,这就是我要求的。

我添加了一个事件监听器:

addEventListener("beforeunload", MyBeforeUnload, false);

我有我的功能:

function MyBeforeUnload() {
    if(!g_IsSaved) {
        return "You have not saved your data. If you leave this page without saving, your data will be lost.";
    }else {
    // Do not return anything if ok to leave
    }
}

这会在IE中触发并显示警告消息。

它会触发该功能并正确返回字符串,但是当使用浏览器右上角的[x]按钮关闭浏览器时,不会在Chrome,Edge或Firefox中触发警告消息。

2017年可以使用这种跨浏览器吗?

1 个答案:

答案 0 :(得分:0)

尝试使用它,因为它应该在跨浏览器中工作

Maven


但Chrome已决定取消在window.addEventListener("beforeunload", function(e) { var confirmationMessage = "You have not saved your data. If you leave this page without saving, your data will be lost."; if (!g_IsSaved) { (e || window.event).returnValue = confirmationMessage; //Gecko + IE return confirmationMessage; //Webkit, Safari, Chrome etc. } else { // Do not return anything if ok to leave } }); 对话框中设置自定义消息的功能,以避免欺诈

请参阅2016年2月18日的bug report

  

onbeforeunload对话框用于现代Web上的两件事:
   1.防止用户无意中丢失数据    2.欺骗用户。

相关问题