如何在没有用户操作(StopPropogation)和window.oneforeunload事件的情况下阻止页面重定向到另一个页面?

时间:2017-08-21 10:12:36

标签: javascript jquery firefox

function confirmExit(e) {

var f = FormChanges();  //checking whether page has been modified or not    

if (f.length > 0){
    if (submitForm == false) {
        if(!e) var e = window.event;
        //e.cancelBubble for IE and it does work
        e.cancelBubble = true;
        e.returnValue = "You have made updates to this page which have not been saved.";
        //e.stopPropagation for Firefox doesn't work.
        if (e.stopPropagation) {
            e.stopPropagation();
            e.preventDefault();
        }       
     }
     }
   setTimeout("enableBeforeUnloadHandler()", "100");
}

} //ignore this

 window.onbeforeunload=confirmExit;
 function enableBeforeUnloadHandler()
{
  window.onbeforeunload=confirmExit;
}

当用户想要在不提交当前页面中的表单的情况下转到其他页面时,会提示是否离开该页面?

但问题是,它在几秒钟内重定向到其他页面而根本没有等待用户操作,我该如何解决这个问题? (我不在Firefox中工作,在IE中工作正常)

1 个答案:

答案 0 :(得分:0)

可能是这样的:

window.onbeforeunload = function() {
    return "Are you sure you want to navigate away?";
}

您实际上需要返回true(更改页面)或false(留在页面上)。