Search code examples
javascriptjqueryjquery-eventsonbeforeunload

How to figure out if the window is closing in the callback of jQuerys unload(); method


Is it possible to figure out just which type of event triggered $(window).unload();? To be a bit more specific, I'm only interested in the event when a user has closed the window (a popup), not when he's just navigating away from the current page.

I've looked into the event parameter passed to the callback of unload(); but there doesn't seem to be anything in that object, that'll be of use.

Does anybody know a solution to this?

Code:

$(function() {
    $(window).unload(function(event) {
        // This is obviously pseudo code. Help me fix it!
        if(event.type == 'closing') { // I had expected something along these lines to work
            // Do stuff before the window closes
        }
    });
});

Solution

  • No, to my knowledge this is not possible. The document only learns that it is about to be unloaded. Why, is the browser's business.