Skip to content Skip to sidebar Skip to footer

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 c

Solution 1:

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

functionpopUpClosed() {
    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.

Solution 2:

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

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

Solution 3:

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;

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


  functioncreate(){

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

Solution 4:

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:

<scripttype="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 :

<bodyonunload="window.opener.popUpClosed();">

Post a Comment for "Refresh Parent Window When The Pop-up Window Is Closed"