How to run function of parent window when child window closes?

前端 未结 7 2217
余生分开走
余生分开走 2020-11-29 02:15

I am calling the javascript window.open() function to load another url in a pop up. Once the users is finished it takes them to the last page that has a link that says close

相关标签:
7条回答
  • 2020-11-29 02:41

    You probably want to use the 'onbeforeunload' event. It will allow you call a function in the parent window from the child immediately before the child window closes.

    So probably something like this:

    window.onbeforeunload = function (e) {
        window.parent.functonToCallBeforeThisWindowCloses();
    };
    
    0 讨论(0)
  • 2020-11-29 02:50

    I know this post is old, but I found that this really works well:

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

    The window.onunload part was the hint I found googling this page. Thanks, @jerjer!

    0 讨论(0)
  • 2020-11-29 02:52

    This is an old post, but I thought I would add another method to do this:

    var win = window.open("http://www.google.com");
    
    var winClosed = setInterval(function () {
    
        if (win.closed) {
            clearInterval(winClosed);
            foo(); //Call your function here
        }
    
    }, 250);
    

    You don't have to modify the contents or use any event handlers from the child window.

    0 讨论(0)
  • 2020-11-29 02:55

    You can somehow try this:

    Spawned window:

    window.onunload = function (e) {
        opener.somefunction(); //or
        opener.document.getElementById('someid').innerHTML = 'update content of parent window';
    };
    

    Parent Window:

    window.open('Spawn.htm','');
    window.somefunction = function(){
    
    }
    

    You should not do this on the parent, otherwise opener.somefunction() will not work, doing window.somefunction makes somefunction as public:

    function somefunction(){
    
    }
    
    0 讨论(0)
  • 2020-11-29 02:56

    The answers as they are require you to add code to the spawned window. That is unnecessary coupling.

    // In parent window
    var pop = open(url);
    pop.onunload = function() {
      // Run your code, the popup window is unloading
      // Beware though, this will also fire if the user navigates to a different
      // page within thepopup. If you need to support that, you will have to play around
      // with pop.closed and setTimeouts
    }
    
    0 讨论(0)
  • 2020-11-29 03:06

    Check following link. This would be helpful too..

    In Parent Window:

    function OpenChildAsPopup() {
            var childWindow = window.open("ChildWindow.aspx", "_blank",
            "width=200px,height=350px,left=200,top=100");
            childWindow.focus();
     }
    
    function ChangeBackgroudColor() {
            var para = document.getElementById('samplePara');
            if (para !="undefied") {
                para.style.backgroundColor = '#6CDBF5';
            }
     }
    

    Parent Window HTML Markup:

    <div>
      <p id="samplePara" style="width: 350px;">
                Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
      </p><br />
     <asp:Button ID="Button1" Text="Open Child Window" 
             runat="server" OnClientClick="OpenChildAsPopup();"/>
    </div>
    

    In Child Window:

    // This will be called when the child window is closed.     
      window.onunload = function (e) {
            opener.ChangeBackgroudColor();
            //or you can do
            //var para = opener.document.getElementById('samplePara');
            //if (para != "undefied") {
            //    para.style.backgroundColor = '#6CDBF5';
            //}
        };
    
    0 讨论(0)
提交回复
热议问题