繁体   English   中英

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

[英]How to stop the page from redirecting to another page without the user action (StopPropogation) and window.oneforeunload event?

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中也可以正常工作)

可能是这样的:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM