Refresh parent window when the pop-up window is closed

后端 未结 4 1865
梦如初夏
梦如初夏 2020-12-14 12:31

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 wh

相关标签:
4条回答
  • 2020-12-14 12:47

    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.
    }
    
    0 讨论(0)
  • 2020-12-14 12:51

    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.

    0 讨论(0)
  • 2020-12-14 12:53

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

    var myPop = "pop up window selector"
    myPop.onunload = function(){ 
      location.reload(); 
    }; 
    
    0 讨论(0)
  • 2020-12-14 12:55

    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();">
    
    0 讨论(0)
提交回复
热议问题