10

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

Gordon
  • 312,688
  • 75
  • 539
  • 559
Kay
  • 229
  • 2
  • 3
  • 7

4 Answers4

17

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.

Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • when the URL is secured mean https: does it is not asking for a window with "really want to submit data" Retry and Cancel buttons? – changeme Jul 11 '12 at 16:30
5

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

var myPop = "pop up window selector"
myPop.onunload = function(){ 
  location.reload(); 
}; 
benhowdle89
  • 36,900
  • 69
  • 202
  • 331
  • +1 but could change `var myPop = "pop up window selector"` to `var myPop = window.open();` to make the example clearer – Gordon Nov 24 '10 at 14:41
  • Sorry, i couldnt think of the selector of the top of my head! Thanks for pointing that out Gordon – benhowdle89 Nov 24 '10 at 14:43
  • This won't work in IE. My answer will. Please stop upvoting it. – Tim Down Nov 24 '10 at 15:12
  • IE will ignore the `onunload` property assigned from another window. You can only do it from within the pop-up. Try it if you don't believe me (I have IE 7, I'm fairly certain IE 8 behaves the same). – Tim Down Nov 24 '10 at 16:15
  • @benhowdle89: Sorry to be aggressive. It's nothing personal. I'm just having a moment of exasperation with the randomness of SO voting and how it doesn't often reward actual knowledge. – Tim Down Nov 25 '10 at 16:25
  • Sorry Tim i cant stop people upvoting this answer. – benhowdle89 Nov 25 '10 at 16:26
  • @benhowdle89: I know, and I am genuinely sorry to have picked on you. – Tim Down Nov 25 '10 at 16:27
  • I dont feel picked on :) just sorry that your getting frustrated! Its baffling why people upvote the wrong answer!? – benhowdle89 Nov 25 '10 at 16:28
2

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.
}
Lenny Markus
  • 3,398
  • 2
  • 24
  • 30
1

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();">
salexch
  • 2,644
  • 1
  • 20
  • 17