Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can you catch a window closing w/ a click of the 'X' button ??

Status
Not open for further replies.

questhaven

Programmer
Mar 28, 2001
81
US
Hi there - Does anyone know if there is any way I can catch the window close event when someone clicks on the "X" window button?
 
No, the browser doesn't differentiate close events. You cannot tell if the browser was closed using 'file->close', X, ALT-F4 or window.close().
 
Thanks for the quick response. What I am trying to do though is have an event get triggered in the parent window by the closing of the child window, if a user clicks on the 'X'. Most people are used to closing windows this way, so I was hoping this would be possible.

For an example of what I am trying to do you can go to and click on the word "Help" on the upper right portion of the screen. Notice how it shrinks the main window down and opens up a help window on the side?

Now click on the X of the "Help" window", notice how the main window resizes to fit the screen?

I am able to achieve this by creating a close button, but I would like to additionally do this. Any ideas anyone?? :)
 
Sorry, my response wans't very clear on that point. You can still catch the window's unload event and act on it. What I was meaning was that if you assign a function to the window's unload event ie:
Code:
<body onUnload=&quot;myCloseFunction()&quot;>

That function will run regardless of how the window is closed, be it by the X, close button, ALT-F4. There is just no way of determining which method the user chose to close the window.
 
Actually, I tried using the onUnload function, however the child window contains a variety of help links, and when clicked they load into the child window. When I use the onUnload method, everytime I click a link in the child window, it closes itself. Do you have any other ideas?
 
Sure, when you open the child window from the main document, it assigns a window handle that you can then use to examine if the child window is still open or if it has been closed, regardless of whether or not the original content of the child window has been replaced.

Example:
Code:
//this script opens a child window and assigns it's handle
//to a variable called mypopup. Then periodically checks
//the state of the popup window to determine if it has been
//closed.
var mypopup = window.open('popup.html', 'mypopup');
checkwindowstatus();

function checkwindowstatus(){
 if(mypopup.closed){
  //The popup has been closed, do some processing here
 }
 else{
  //check again in one second
  self.setTimeout('checkwindowstatus()', 1000);
 }
}

 
Thanks a lot for your help! I'm going to try this and see if it works out for me. I **REALLY** appreciate your time!

~ Michele
 
dwarfthrower - You are the BEST forever!! Thank you so, so, SO much for your help!! :) This totally worked! :)

- Michele
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top