Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: How to disable close button on window?


Message #1 by Kavanjit Singh <kavanjit.singh@i...> on Wed, 21 Mar 2001 17:46:47 +0530
> 
> Hi! 
> 
> I am opening a new window useing window.open method. 
> however i dont want users to be able to close that newly window by using
> close(X) button on top. i want to disable that button... 
> 
> how can it be done? 
> 
> 
> Ricky

You can't disable the button, but you can check the closed property of 
your new window object and re-open it if it is closed, i.e.

// this code goes in the page which opens the pop-up
// 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();

The trick here is getting the window to close, which is why I've put a 
"switch" in here called closeMe. This can be set to 1 by the popup by 
using a function like:

function Closeme(){
	window.opener.closeMe = 1;
	self.close();
}

Now, all you need to do is have a button in the pop-up which activates the 
closeMe() function - this will set the switch to 1 (true) in the opening 
window, and the next pass of checkClosed() will clear the timeout and 
break out of the loop.


  Return to Index