简体   繁体   中英

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)

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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