Click to See Complete Forum and Search --> : Closing a remote popup window


cdonnel
09-25-2003, 11:42 AM
I need help with a javascript error when I try to close a popup window.

The window is opened with a function "go" that is called from the main window with a link to the contents of the popup window. A link in the popup window uses the function "goBack" to get back to the main window. This function includes a Timeout to close the popup after 10 seconds.

This seems to work well as long as you return to the main window via a link in the popup. However, if you leave the popup by just clicking on the main window, the popup remains open in the background. I've tried two fixes, neither of which was satisfactory:

(1) onBlur="window.close()" in the body tag of the popup. This caused the popup to close when internal links (such as "top of page" or "print page") were clicked.

(2) onFocus="winPop.close()" in the body tag of the main window. This worked, but it caused a javascript error, because the first time the page is loaded there is no popup window to close.

I think what I need is something in the main window to say, "If winPop exists and if it is open, close it."

If you can help, please be very specific: Exactly what is the script, where does it go, and how is it called?

Thank you.
Carol

Webskater
09-25-2003, 02:55 PM
if (typeof(winPop)=="object") { winPop.close() }

cdonnel
09-25-2003, 04:51 PM
Thanks for the code, Webskater. Sorry to be so dense, but it would help if you could tell me:
(1) What do I put where you have "typeof"?
(2) What do I put where you have "object"? Do I substitute the word "window" or the name of the window, or something else?
(3) Where do I put this code and where do I put the statement that executes it?

It may help for you to see the code that opens the window:
============================
var winPop = null;
function go ( url ) {
winPop = window.open("", "popup", "width=400,height=300");
winPop.location.href = url;
winPop.focus();
if (winPop.opener == null) {
winPop.opener = window;
}
}
=============================

The window is opened by clicking on a link, as follows:

<a href="javascript:go('moreinfo.html')">Get more information.</a>

However, I don't want to close the window by clicking a link; I want it to close automatically (after a reasonable delay) when it is abandoned.

Thanks,
Carol