0

The below code works in FF but in chrome, browser_close_event() is not working. The browser_close_cancel_event() is working though.

The both browser_close_event() and browser_close_cancel_event() functions will fetch ajax call when called.

How can i call a function when user click the 'leave' button in browser close alert.

$( document ).ready(function() {

   var oldMousemove = document.body.onmousemove,
        onCancel = function () {
            // Do something if the user stays.
            browser_close_cancel_event();
            document.body.onmousemove = oldMousemove;
        };

    window.onbeforeunload = function() {
        // if user click leave
        browser_close_event();
        document.body.onmousemove = onCancel;
        return 'Do you really want to leave?';
    };


});
  • Depends on what you are trying to do. There are restrictions on what you can do in `onbeforeunload` due to abuse and poor user experience over the years. Provide a proper explanation and a [mcve] – charlietfl Jul 18 '17 at 12:56
  • "The functions will fetch ajax call when called" -- yeah, you can't do that in a `onbeforeunload`. – Daniel Beck Jul 18 '17 at 20:00
  • Then how can i do this in chrome? Ajax via function call in onbeforeunload. is working in FF – Md. Shamvil Hossain Jul 19 '17 at 02:53
  • You can't. (And shouldn't. Delaying the closing of a browser window so you can complete an ajax request is really poor UX.) It's best not to depend on `onbeforeunload` for anything other than "are you sure you want to close the window" prompts (and not all browsers even support that much.) See the Notes section here: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload – Daniel Beck Jul 19 '17 at 16:29

0 Answers0