1

I want to check if any textareas in a page were changed, and if so, if the user navigates away without submitting, to stop the navigation and promp a modal window (instead of the one from onbeforeunload) to check if the user wants to realy leave the page or stay and submit the data.

So far I've made this:

var needToConfirm = false;

$("input,textarea").on("input", function () {
    needToConfirm = true;
});

$("select").change(function () {
    needToConfirm = true;
});
function closeEditorWarning() {

if (needToConfirm) {
        var t = News;
        t.config.modalTitle.html("If you exit this page, your unsaved changes will be lost"), t.config.modalMsg.text("Are you sure you don't want to submit the changes?"), t.config.modalWindow.show(), t.config.modalAcceptBtn.on(click, null)
        return "null";
    }

}

window.onbeforeunload = closeEditorWarning();

With this I get the modal popup showing but then the page gets unloaded to the one the user selected right away ignoring the modal window. So how do I prevent the page from being unloaded and make it wait for the user to confirm he wants to leave without submitting the changes?

Micael Florêncio
  • 407
  • 1
  • 5
  • 11
  • possible duplicate of [How can I override the OnBeforeUnload dialog and replace it with my own?](http://stackoverflow.com/questions/276660/how-can-i-override-the-onbeforeunload-dialog-and-replace-it-with-my-own) – Ansel Santosa Jan 21 '15 at 20:23

1 Answers1

1

This is not possible. The only way to interrupt page unloading is to return a String message in the onbeforeunload handler (like this question).

If what you're asking for what possible then pages could stay open indefinitely despite the user's requests to close them (obviously, not good behavior).

Community
  • 1
  • 1
Ansel Santosa
  • 8,224
  • 1
  • 28
  • 39
  • Then is there some other handler that can be used? Or some other way to prevent/pause the unloading? I used there the onbeforeunloa because it's the only way I know at the moment to do it. But I wanted to replace the window it shows with the one of modal window because that's what's used for other warnings or messages around the site – Micael Florêncio Jan 21 '15 at 17:47
  • I understand what you're trying to do. It's just not possible. There is a reason that you don't see the behavior you're describing on any sites. They all use native alerts because that's the only way and that will never change. Sorry. – Ansel Santosa Jan 21 '15 at 19:18