< Back

Document Subject: Check if window.opener is still open before proceeding
Hint Short Cut: Add this to your code & documentation to help you find this page.
http://#OpenerStillOpen or http://A555F9/nn.nsf/ByAlias/OpenerStillOpen

I have been doing some code, (sounds like a problem), for a company's web site. The site opens various sub windows and each has a close button that updates the parent or opener window. Following the first law of demonstration, I closed the windows in the wrong order, and the close button on the child window would not close the window. Very annoying.




So why would the close button and routine no longer work?

Here is the close button code:

<input type=button value="Close"  onClick="javascript:closeWindow();">;

It was because the javascript came up with an error:

Here is the function which was stored in the form's "JS Header" area:

function closeWindow(){
window.opener.rdocprintClosed=true;
window.opener.docprintClosed=true;
window.close()
}

The code above never got to the window.close() as the lines above failed.

here is the code I changed to:

function closeWindow(){
op=window.opener;
if (op && op.open && !op.closed) window.opener.rdocprintClosed=true;
if (op && op.open && !op.closed) window.opener.docprintClosed=true;
window.close()
}

 

Why the three checks?

The checks make sure the window pointer exists, the window was open at some point and the window is not closed.