View Full Version : manipulating the window events
biznusman
09-25-2002, 09:18 PM
I need to know if there is a way to capture the closing of a browser window. I mean is there an event that is fired when you close the browser window? Thanks.
ShriekForth
09-25-2002, 09:42 PM
onUnload in the body tag, or set on the window can be used as a triger, nice for child windows. onBeforeUnload is nicer, but only supported in IE. Of course if you have a child and a parent, the parent can see if the child is closed.
myWindow = window.open("my.html")
if (!myWindow .closed){
myWindow .close();
}
ShriekForth
Roy Sinclair
09-25-2002, 10:08 PM
There is no event you can watch to tell you that the browser is closing. The onunload events happen when you change web pages as well which makes them useless for that kind of checking.
ShriekForth
09-25-2002, 10:37 PM
I've seen it cheated with a frameset. One small framset (0 height)does nothing but wait for onUnload. The otherframeset works normally. But if someone were to follow a link out of your site you could still track it until something broke them out of the frameset.
Part of the DOM 2 reference is window.onclose, but I don't know how well that is actually supported in the new browsers. I'm going out of town, and don't have time to test it it. You could have a look though.
Mozilla DOM (http://www.mozilla.org/docs/dom/domref/dom_window_ref.html#1011624)
ShriekForth
adios
09-26-2002, 02:16 AM
You can pop up a 'spy' window, 100x100, which hangs around just long enough to check the .closed property of the opener, seeing if it's set, and doing something if it is. This makes window.onunload/onbeforeunload(IE) somewhat less useless for this purpose.
glenngv
09-26-2002, 04:20 AM
<html>
<head>
<title>Detecting Closing of Window in IE</title>
<script language="javascript">
function doUnload()
{
if ((window.event.clientX < 0) && (window.event.clientY < 0))
{
alert("The window is closed...");
}
}
</script>
</head>
<body onunload="doUnload()">
<h3>Detecting Closing of Window in IE</h3>
Do the 3 ways below and see the difference:<br>
1. Try to refresh the page.<br>
2. Try to type any URL in the address bar.<br>
3. Try to close this window by clicking X button of this window.
</body>
</html>
biznusman
09-26-2002, 03:32 PM
I appreciate it. Glenn's code works beautifully for what I need.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.