1

I am using window.onbeforeunload method to show confirm message if user leaves website.

window.onbeforeunload = function(e) {
    return textMsg;
};

But I need this to be fired only if user navigates to external web-site or closes window.

So how to cancel this confirm message for all XHR inside same domain and form submit etc?

vickk
  • 119
  • 1
  • 9
  • Possible duplicate of [OnUnload message needed for external links](http://stackoverflow.com/questions/2921206/onunload-message-needed-for-external-links) – KyleMit Apr 13 '17 at 12:22

1 Answers1

1

Try (untested code):

window.onbeforeunload = function(e) {
    return textMsg;
};

$('a:not([href^="http"])').on('click', function() {
    window.onbeforeunload = null; // prevent message
});

$('form').on('submit', function() {
    window.onbeforeunload = null; // prevent message
});

This will prevent the event from triggering if links do not start with http (external links) and for <form> submits. Looking for a solution for window closing at the moment.

D4V1D
  • 5,805
  • 3
  • 30
  • 65