How to trigger a browser window or tab close event with jQuery

jquery

Solution

You can use `unload()` on the `window` property in jQuery:

$(window).unload(function() {
   //do stuff
});

You don't need jQuery to do it though, you can use good ol' fashioned JavaScript:

window.onbeforeunload = function(e){
    var msg = 'Are you sure?';
    e = e || window.event;

    if(e)
        e.returnValue = msg;

    return msg;
}

Problem

Is there any way to trigger a window/tab close event with jQuery. I already tried with ``` $('selector').unload() ``` But it didn't work.

Original source

Related problems