1

This is something that's been driving me nuts.

I'm trying to detect whether the uses closes or navigates away from a page in order to do an ajax response upon the event. I have tried almost every possible method to invoke this but no luck. The only thing I can think of is that the activate window in question was fired using: window.open() method. Could that cause any issues? What I have so far:

window.onbeforeunload = function() {
   //ajax stuff here
};

However, I've noticed that this does not work after the page is fully loaded. The event fires within the first few milliseconds (if I open the window and try to close it right away) during the page load and won't work after that.

Any ideas?

2 Answers 2

1

I once ran into this issue, and found it worked for me only by setting async : false on the ajax, like this:

jQuery(window).bind('beforeunload', function () {
    jQuery.ajax({
        url: 'your.url',
        async: false,
        data: yourdata
        timeout: 2000 // or whatever timeout in milliseconds you want
        success: function(data){
            // Do whatever you want
        }
    });
});

As Ian mentioned on the comments, it's a good idea to set a timeout to prevent the window for taking too long to close in case the request takes a while. And keep in mind it won't work in all browsers...

Sign up to request clarification or add additional context in comments.

4 Comments

You also might want to set the timeout option so that it doesn't prevent the window from never closing (or taking a long time) if the request takes awhile. Also, this won't work in every browser, but I'd say it's the best/only option
This is a nice clean way of doing this. Although the issue is that the beforeunload does not fire at all..
@DimitriTopaloglou I'm confused on what you're binding to. You said you're opening a window with window.open()...so are you using var win = window.open("", "", ""); jQuery(win).bind("beforeunload", function () { });? And I'm confused when it's firing, and where. You said it fires within the first few milliseconds...of what?
I open a window using window.open() with its properties I chose. Now, I want to be able to detect whenever this newly opened window (the one the user is working on) is closed using the 'X' button or if the user navigates away. All the methods I've used, including the answer given, do not work for what I'm trying to do. The ONLY way they work is if I try to close the newly opened window within the first few milliseconds and before anything loads in that page. After that, it doesn't work. That's the issue.
1

This also supports old versions of IE and Firefox.

window.onbeforeunload = function (e) {
  var message = "Your confirmation message goes here.",
  e = e || window.event;
  // For IE and Firefox
  if (e) {
    e.returnValue = message;
  }

  // For Safari
  return message;
};

1 Comment

I still have no luck with this. I'm not sure why this is happening. I've done this in the past with no issues..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.