Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: want to trap event on window.close


Message #1 by vpandey@i... on Wed, 26 Sep 2001 05:31:22
I did a script for someone a while ago (may even have been this list) who 
wanted to open a non-closing window. I came up with something which tests 
whether the window os open or not using a timeout and the closed property 
of the window. Here's the code:

This is the page which opens the window:

<html>
<head><title>Untitled</title>
<script type="text/javascript" language="JavaScript">
// set globals for window object, close switch, and timeout
var reopen;
var closeMe = 0;
var newWin = window.open("blank.htm","blank","width=100,height=100");
function checkClosed(){
    //check to see if the pop-up has been closed
    if(newWin.closed){
        //if it has been closed, check to see how it was closed
        if(closeMe){
            //if this switch is set, break out of the loop
            clearTimeout(reopen);
            return;
        }
        //switch is not set, so the user has closed the window with
        //the close(X) button - re-open the window!
        newWin = window.open("blank.htm","blank","width=100,height=100");
    }
    //window is still open, so check again in a little while
    reopen = setTimeout("checkClosed();",100);
}
checkClosed();
</script></head>
<body>
</body>

This is the page which loads in the new window (blank.htm)

<html>
<head>
<title>Untitled</title>
<script>
function Closeme(){
        // this toggles the switch to indicate the window has been closed
        // using the button (rather than the window control)
	window.opener.closeMe = 1;
	self.close();
}
</script>
</head>
<body>
<input type="button" onClick="Closeme()" value="close">
</body>
</html>

I think you could adapt this to suit your needs without too much trouble.

  Return to Index