views:

68

answers:

2

UPDATE

So after reading both of your answers I realize there is no reliable way to determine when a browser window is closed. Originally I was planning to use this to unlock a record in the database when the page is closed. Basically when the user loads the page the record it's accessing locks and then unlocks when the page is closed. Any suggestions on how to do this differently/better? I was thinking about just posting a notice on the page that they have to use a close button to close the form rather than the X, and if they do close using the X, the record stays locked for say 5 minutes until they can access it again.


This code is very simple because I was just testing it out. Basically when I run this page in IE and Chrome, the alert fires just fine. When I run it in Firefox, in only a single tab, and close the entire browser, the alert never fires. However, if I have multiple tabs and open the page as a new tab and then close it using the X on the tab, the alert fires. Does anyone know what Firefox is doing back there? I feel like Will Ferrell in Wedding Crashers never knowing what his mom is doing in the kitchen.

Any help is appreciated, because having to have multiple tabs open just to get onunload to fire is pretty undesirable.

<html>
<head runat="server">
    <title></title>
    <script type="text/javascript">

        window.onunload = function() {
            alert("unload event detected!");
        }

    </script>
</head>
<body>
</body>
</html>
A: 

Firefox closes immediately after you click the X to close the browser. For some reason it 'forgets' to call the onunload event.

Joel
'forgets' to call it, or intentionally doesn't call it? I, for one, would not like a page interfering with my intentionally terminating the window or the whole program, so it could very well be a conscious decision by the developers not to fire the event.
Stephen P
What @Stephen said. This has been abused badly in the past by unscrupulous sites (once upon a time, setting IE's "home page" to whitehouse.com on lab computers was a pretty good way of embarrassing new users...)
Shog9
Thanks for the help, please check out my update to the post.
ryanulit
+2  A: 
T.J. Crowder
Thanks for the help, please check out my update to the post.
ryanulit