0

Question is clear, I need to make an ajax call when user clicks on browser close button, but before closing the window.

Any possible solution for this?

I dont need any confirmation box etc. just a trigger for ajax and then close the window.

user2398514
  • 137
  • 4
  • 17
  • 2
    I've tried making a synchronous AJAX call when the window closes, and sometimes it worked, sometimes it didn't. It seems browsers don't like JavaScript code that blocks a window from closing, except for cases like `alert` or `confirm`. – Greg Burghardt Dec 10 '14 at 18:15
  • @GregBurghardt : so you say there is a chance for doing alert? I tried to do alert but it comes with confirmation box, and an error in console says alerts before onunload event are blocked :( – user2398514 Dec 10 '14 at 18:19
  • onbeforeunload and beforeunload don't work ? what have you else you tried ? – Billy Dec 10 '14 at 20:05

1 Answers1

0

An answer from How to show alert message when closing a window? should work here:

<script type="text/javascript">
  window.onbeforeunload = function() {
      $.ajax(...);
  }
</script>
Community
  • 1
  • 1
Phil Tune
  • 3,154
  • 3
  • 24
  • 46