Any idea how to catch event when web page closed only by button close:

Any idea how to catch event when web page closed only by button close:

You can use the beforeunload event.
Try this sample code
window.onbeforeunload = function (e)
{
e = e || window.event;
var y = e.pageY || e.clientY;
if (y < 0){
return "Do You really Want to Close the window ?"
}
else {
return "Refreshing this page can result in data loss.";
}
}
window.onbeforeunload = function() {
return "Bye now!";
};
Just use simple working code
You cannot detect only that event.
The only way to detect window closing or tab closing ( user want to get out of your website ) is to use the onbeforeunload and onunload javascript events.
These events are triggered when you go back or when you click a link too, you can check that if you want. Also window.showModalDialog(), window.alert(), window.confirm(), and window.prompt() can misbehave during this process as you see here MozillaDeveloper.
There is no specific event for capturing browser close event, but you can use unload event of the current page.
$( window ).unload(function() {
alert( "Bye now!" );
});
Updated:
$(window).on('beforeunload', function(){
return 'Are you sure you want to leave?';
});
$(window).on('unload', function(){
alert("Yes");
});