Important alert: (current site time 7/15/2013 1:31:20 PM EDT)
 

VB icon

Close child window when site unloaded from parent

Email
Submitted on: 6/5/1998
By: Found on the Web 
Level: Not Given
User Rating: By 2 Users
Compatibility: JavaScript
Views: 12524
 
     How can do you close a child window when your site is unloaded from the parent window? Found at: http://www.irt.org
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Close child window when site unloaded from parent
' Description:How can do you close a child window when your site is unloaded from the parent window? 
 Found at: http://www.irt.org
' By: Found on the Web
'
' Assumes:Note, that this has not been tested, and will need further work to make it robust. The onerror event handler was only introduced in Netscape 3 (I'm not sure about Internet Explorer). Also you may need to check if the location of the main window, although still on the same server, has not actually gone onto another site on the same ISP
'**************************************

Normally you close a child window using an onUnload event handler in the BODY tag of the window that creates the child window, e.g.: 
<BODY onUnload="childWindowName.close()">
 
This will however close the child window even if you are just navigating to another page in your site. Keeping it open whilst still within your site is going to more difficult. 
What you could do is use the onUnload event handler to trigger a checking mechansim in the child window that checks the location of the newly loaded document in the main window. If its on your own server then you'll be able to check the location, if its not on your server you'll not be able to check the location, and you'll get an error. You may be able to trap this error and thus close the window using self.close(). 
You'll need to trigger a checking mechanism that will allow a delay for the main window to change its location, so something like: 
<BODY onUnload="childWindowName.functionName()">
 
And then in the child window: 
<SCRIPT LANGUAGE="JavaScript"><!--
functionName() {
setTimeout("checkparent()",2000); // a 2 second delay
}
function closeWindow() {
self.close();
}
function checkparent() {
self.onerror = closeWindow;
parentWindow = opener.location.href;
}
//--></SCRIPT>


Other 219 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Not Given category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.