How can i trigger a javascript alert when user close browser tab / browser in all web browser ( chrome / Firefox )
Asked
Active
Viewed 1,302 times
0
-
catch `onbeforeunload` event. https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload – Suresh Atta Dec 26 '17 at 06:03
-
https://stackoverflow.com/questions/3888902/detect-browser-or-tab-closing – zabusa Dec 26 '17 at 06:04
2 Answers
1
You can use onbeforeunload
event on window
object.
you can go through this answer for browser compatability.
window.onbeforeunload = function(e) {
return 'Close Tab?';
};

Rohit Agrawal
- 1,496
- 9
- 20
-
the above script doesn't work on my firefox as many stackoverflow answers pointed out this one is there any clear example you can share – muthukrishnan91 Dec 26 '17 at 06:09
-
-1
Handle onunload
& onbeforeunload
events and call alert
in the event handling function

Chandre Gowda
- 870
- 1
- 7
- 24