Any idea how to catch event when web page closed only by button close:
Asked
Active
Viewed 1,215 times
1
-
There are no option for this: http://stackoverflow.com/questions/16707249/detect-close-windows-event-by-jquery – vaso123 Nov 03 '14 at 12:48
4 Answers
1
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.";
}
}
Sunil Hari
- 1,648
- 1
- 11
- 20
1
window.onbeforeunload = function() {
return "Bye now!";
};
Just use simple working code
Rohit Batham
- 1,238
- 1
- 9
- 13
1
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.
Razvan Dumitru
- 10,549
- 4
- 31
- 50
1
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");
});
Satinder singh
- 9,714
- 16
- 57
- 99