Kay
Kay

Reputation: 229

Refresh parent window when the pop-up window is closed

Is there any way I can refresh the parent window when a popup window is closed without adding any javascript code to the popup window?

I have a page parent.php on which users can click "open popup" to open a popup window. This popup window shows some flash content and its not possible for me to add something like

window.onunload = function(){ 
  window.opener.location.reload(); 
}; 

to the popup window page markup.

Is there any other method to achieve this? Thanks

Upvotes: 10

Views: 31345

Answers (4)

Lenny Markus
Lenny Markus

Reputation: 3418

The problem with Tim Down's method is that it doesn't answer the original question. The requirement is that you cannot add any code to the pop-up window.

One solution that I've found, while not particularly elegant, is effective across all browsers I've tested on.

You will be simply polling the newly created window object continuously, checking if it's still open.

On parent window:

  var register;
  var poll;

  function isOpen(){
      if(register.closed){alert("Closed!"); clearInterval(poll);}
  }


  function create(){

      register = window.open("http://www.google.com","register","width=425,height=550");
      poll=setInterval("isOpen()",100); //Poll every 100 ms.
}

Upvotes: 2

salexch
salexch

Reputation: 2704

had similar problem to detect the closing popup in the parent window. I think the reason was in not setting the document.domain property.

any way add to Tim Down answer the document.domain property for both window and popup like this:

    <script type="text/javascript">
        document.domain='<?=$_SERVER['SERVER_NAME']?>';
    </script> 

and instead of

 window.onunload = function() {
     if (window.opener && !window.opener.closed) {
        window.opener.popUpClosed();
     }
 };

I used in the popup :

     <body onunload="window.opener.popUpClosed();">

Upvotes: 1

Tim Down
Tim Down

Reputation: 324477

To make this work in all major browsers, you need to handle the unload event handler in the pop-up and do the reloading in the main window. In the main window, add

function popUpClosed() {
    window.location.reload();
}

In the pop-up:

window.onunload = function() {
    if (window.opener && !window.opener.closed) {
        window.opener.popUpClosed();
    }
};

So the answer to your question is generally no, if you need your code to work in all browsers, in particular IE.

Upvotes: 17

benhowdle89
benhowdle89

Reputation: 37454

I'm sure you can just add this to parent.php:

var myPop = "pop up window selector"
myPop.onunload = function(){ 
  location.reload(); 
}; 

Upvotes: 5

Related Questions