简体   繁体   中英

window.onbeforeunload returns user to same page

On leaving a page with a form, I want to save the changes made without any user confirmation.

Having found this question I adapted one of the answers as follows. Note that I am not using return:

function setConfirmUnload(on) {
    window.onbeforeunload = (on) ? unloadMessage : null;
}
function unloadMessage() {
    alert('Gonna save now');
    setTimeout(validate_submit,500);
}

This is only triggered when the user fills some value in the form. If an element value changes, I use

setConfirmUnload(true);

Result in FF23: when user clicks somewhere to leave the page, the alert is shown, validate_submit() is executed on "OK", new page appears - BUT now the alert reappears and the user is returned to the original page on "OK". Why? Can somebody confirm this behaviour?

The window.onbeforeunload method causes a confirm box to appear containing whatever message is returned by the function assigned to it. You can put alerts and other stuff in this method, but it will always end with a confirm message. If your function returns nothing, it will still give a message (determined by browser) saying "are you sure you want to leave this page" or something along those lines.

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