Does anyone know any way that I can use javascript to check when the browser window is closed and pop-up a confirmation dialog to ask whether the user is confirm to exit the browser or change his mind to stay?
-
By browser window do you mean the page window or the entire browser? – Joe Phillips Apr 30 '09 at 05:38
-
possible duplicate of [Browser window close event](http://stackoverflow.com/questions/1631959/browser-window-close-event) – Danubian Sailor Mar 28 '14 at 07:31
5 Answers
window.onbeforeunload = function (e) {
var e = e || window.event;
//IE & Firefox
if (e) {
e.returnValue = 'Are you sure?';
}
// For Safari
return 'Are you sure?';
};

- 44,326
- 9
- 65
- 80
-
-
1@Anon nope, confirm() returns a boolean, your handler for onbeforeunload is required to return a string. – Chad Grant Apr 30 '09 at 05:47
-
Well, that's a peculiar behavior. Goes to show how often I've used it! – Anonymous Apr 30 '09 at 05:50
-
I am down-voting this because if you try going to another link on the page, you see "Are you sure?" – ElHaix Oct 09 '15 at 18:51
-
there is no difference between navigating away from a page and closing a browser window as far as the DOM is concerned. @ElHaix – Chad Grant Oct 11 '15 at 17:16
-
Yes... That's the problem. So what if you have kinks on that page, etc.? User will be shown the pop-up if they click on a link. It should only occur if they close the page. I got around this by checking history on new browser page load but that's weak as they could go to other sites before the checking page. – ElHaix Oct 11 '15 at 21:45
-
I am using this script for exit popup and popup is as i need... but i need only on exit the site not when user change page of click any other link ... – Mayur Devmurari Dec 03 '15 at 05:02
-
beforeunload is no longer supported in ios. any other solution? – Kalleshwar Kalshetty Apr 26 '21 at 16:46
The documentation here encourages listening to the onbeforeunload
event and/or adding an event listener on window
.
window.addEventListener('beforeunload', function(e) {}, false);
You can also just populate the .onunload
or .onbeforeunload
properties of window
with a function or a function reference.
Though behaviour is not standardized across browsers, the function may return a value that the browser will display when confirming whether to leave the page.

- 4,902
- 4
- 22
- 31
This works too, unless for IE8
$(window).bind('beforeunload', function (e) {
// code to execute when browser is closed
e.$.post("func.php", { action: 'action', id_userMsg: '<?php echo $id_user; ?>' });
});

- 723
- 3
- 7
- 24
If the browser remains running after the page is closed, and if the browser processes the "onbeforeunload" event of the body element (sometimes it's disabled), and if the browser allows popup windows or mesage boxes and the ability to return false from that event to prevent the page change, then it's possible.
For an example, start typing a comment on any stackoverflow page with Javascript enabled and then navigate away from that page.

- 49,213
- 1
- 25
- 19