如何使用模态窗口防止页面卸载

时间:2015-01-21 17:35:00

标签: jquery modal-dialog

我想检查页面中是否有任何textareas被更改,如果是,如果用户导航而不提交,则停止导航并提示模态窗口(而不是onbeforeunload中的那个)来检查用户是否想要真正离开页面或留下并提交数据。

到目前为止,我已经做到了这一点:

var needToConfirm = false;

$("input,textarea").on("input", function () {
    needToConfirm = true;
});

$("select").change(function () {
    needToConfirm = true;
});
function closeEditorWarning() {

if (needToConfirm) {
        var t = News;
        t.config.modalTitle.html("If you exit this page, your unsaved changes will be lost"), t.config.modalMsg.text("Are you sure you don't want to submit the changes?"), t.config.modalWindow.show(), t.config.modalAcceptBtn.on(click, null)
        return "null";
    }

}

window.onbeforeunload = closeEditorWarning();

有了这个,我得到模态弹出窗口显示但是然后页面被卸载到用户选择的那个忽略模态窗口。那么如何防止页面被卸载并等待用户确认他想要离开而不提交更改呢?

1 个答案:

答案 0 :(得分:1)

这是不可能的。中断页面卸载的唯一方法是在String处理程序(like this question)中返回onbeforeunload消息。

如果您要求什么可能,那么尽管用户请求关闭它们(显然,这不是好行为),页面仍可无限期保持打开状态。

相关问题