views:

68

answers:

3

This code will open go.com in popup window:

<a href="javascript:void(0)" onClick="javascript:window.open('http://go.com','','status=yes,scrollbars=yes,toolbar=no,menubar=no,location=no ,width=300px,height=300px')">open</a>

and I want to use alert('closed!') in main page, when popup is closed and I can't edit popup page for use onunload="alert('closed!')", because the popup page is an external page and I can't change the code like

var mywindow = window.open('http://go.com') ...

Thanks.

A: 

How about checking the status of the window every 1 second to see if it has been closed? Sample code, not tested:

var mywindow= window.open();
checkmywindow();

function checkmywindow()
{
   if(mywindow.closed)
   {
      //code when popup is closed
   }
   else
   {
      setTimeout("checkmywindow();", 1000);
   }
}
Trevor
A: 

How about instead of directly loading that external page in the window, you open up a "local" window with an iframe containing the external page content? That way you still have enough control over the actual window to be able to tell when it's closed.

Some drawbacks: the address bar will not show the URL they are browsing and it may also break if go.com has some sort of frame busting code.

Some example code:

function openIFrameInWindow(url) {
    var myWindow = window.open("");

    var iframe = document.createElement("iframe");
    iframe.src = url;

    myWindow.document.body.appendChild(iframe); 

    myWindow.document.body.onbeforeunload = function () { alert("WINDOW CLOSED") }; 
}

<a href="javascript:void(0)" onClick="javascript:openIFrameInWindow('http://go.com')"&gt;open&lt;/a&gt;
CD Sanchez
A: 

When the popup window is closed you can INFORM the opener window about it by using the window.opener property.

So:

//pseudocode in pop-up window
onClose() {
 window.opener.popUpClosed = true;
}

Now in your page that opened the popUp you can check for the value of the popUpClosed variable.

More on the opener property here: http://www.webreference.com/js/tutorial1/opener.html

andreas
He doesn't have access to the window because it's an external page.
CD Sanchez
The opener property allows access to the page that opened the window. This is what i think that is described in the question
andreas
@andreas: Yes.. but only if you have access to the page that's being opened. I doubt he has access to modify scripts on go.com.
CD Sanchez