3

I'm trying to do a jquery .ajax synchronous call on window.onunload. FF and IE works but not on Chrome. I had done some testing and Chrome seems to terminate the window and not wait for the response.

Here is my code:

window.onunload = function() {
    $.ajax({
        url: "/someurl",
        async: false,
        data: ({ somedata: "test" }),
        success: function(){ return true; },
        error: function(){ alert('got some error'); }
    });
}

Note that I'm using window.onbeforeunload to display a warning message, then trigger the window.unload when the user leaves the page.

deepsweech
  • 31
  • 4

1 Answers1

1

Use onbeforeunload instead of onunload, and that should do the trick. You need to wrap both of your events into that one action. onunload won't fire in Chrome.

Dan Short
  • 9,598
  • 2
  • 28
  • 53