0

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?

Community
  • 1
  • 1
user2317194
  • 221
  • 1
  • 4
  • 13

1 Answers1

0

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.

Will P.
  • 8,437
  • 3
  • 36
  • 45
  • This is true for my situation only when I use return ''; - whereas using return null; shows same result as above (i.e. no message box). – user2317194 Aug 08 '13 at 18:17