简体   繁体   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;
}

When the user wants to go to some other page without submitting the form in the current page,it prompts whether to leave the page or not? 当用户想转到其他页面而不在当前页面提交表单时,它会提示是否离开该页面?

But the problem is, it redirects to the other page in a couple of seconds without waiting for the user action at all,How do i fix this? 但是问题是,它可以在几秒钟内重定向到另一个页面,而根本不等待用户操作,我该如何解决? (I doesn't work in Firefox,in IE it works fine) (我无法在Firefox中工作,在IE中也可以正常工作)

Could be like this: 可能是这样的:

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

You actually need to return true (change page) or false (stay on page). 您实际上需要返回true (更改页面)或false (停留在页面上)。

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

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