Reputation: 13616
Any idea how to catch event when web page closed only by button close:
Upvotes: 0
Views: 1357
Reputation: 10198
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");
});
Upvotes: 1
Reputation: 12452
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.
Upvotes: 1
Reputation:
window.onbeforeunload = function() {
return "Bye now!";
};
Just use simple working code
Upvotes: 1
Reputation: 1776
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.";
}
}
Upvotes: 1