Click to See Complete Forum and Search --> : window.close() Problem with a twist!


Brrrian
11-10-2003, 12:01 AM
I'm chasing a window.close() issue. The Main page opens a child window which populates with a menu. Choosing a menu item opens the menu selection in the parent window. This is working well with these two scripts:

function popUp(page, name, details) {
newWin=window.open(page, name, details);
newWin.focus();
return false;
}

function loadinparent(url){
self.opener.location = url;
}


The user can continue to make choices from the Menu... each time populating the Parent Window until the user is done working. At that point they Click LogOut in the Main Window.

----------------
The Problem
-----------------
When the user Clicks LogOut in the Parent window after working awhile I need to Close the Child Window. I can't get anything to work.

onclick="theChildWindowName.close();"
onclick="child.close();"
onclick="newWin.close();"

The Parent seems to loose track of the Child window. I get errors like theChildWindow is undefined... or newWin is undefined.

I spent the weekend surfing javascript forums etc... no luck yet! Help! ;)

Gollum
11-10-2003, 02:27 AM
Indeed, every time a browser window loads a new page, all traces of the previous pages are wiped. This is a security measure - imagine if some javascript code from some hacker's website managed to lodge itself in your browser waiting for you to say navigate to your internet banking site or something...

Anyway, to fix your problem you need to think your way around the problem. You could:
1. set up a timer loop in your popup like this...

function reuniteWithParent()
{
try
{
opener.theChild = self;
}
catch(e) {}

// if the parent window has gone, might as well close here too
if ( opener.closed ) close();

setTimeout("reuniteWithParent();", 50);
}
reuniteWithParent();

so twenty times a second (or so) you are reminding the parent window that the popup exists and how to get to it. Beware that there is a race condition here.

or
2. Swap roles. Have the parent window direct where the popup window goes. That way the parent window will always have a link to the child.