43

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 window that calls the window.close() function. Now when that page closes I need to update something in the original page that opened the window. Is there any way to do this? I have to call a function that is in my original page.

tshepang
  • 12,111
  • 21
  • 91
  • 136
ngreenwood6
  • 8,108
  • 11
  • 33
  • 52

7 Answers7

64

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

}
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
jerjer
  • 8,694
  • 30
  • 36
10

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.

9

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();
};
Darrell Brogdon
  • 6,843
  • 9
  • 47
  • 62
  • ok that looks like what I am looking for but how will it know what window I am talking about. For instance if I do window.open() and then i add that below it will it know its that window or how do i attach it to that window? – ngreenwood6 Nov 22 '09 at 05:37
  • You'll put that onbeforeunload in a script tag in your child window's HTML. Since the child window only has one parent the browser automatically knows what that parent window is. – Darrell Brogdon Nov 22 '09 at 05:41
  • so if I have a function called doSomething() in the parent window but its not defined in the child window it will still allow me to run it? – ngreenwood6 Nov 22 '09 at 05:44
  • Yep. You'd do something like 'window.parent.doSomething()'. Though you may want/have to use the apply() JavaScript function (http://www.javascriptkit.com/jsref/function.shtml) to actually make the call to the parent function. – Darrell Brogdon Nov 22 '09 at 05:52
  • Thanks for the help. You go me in the right place but I accepted the other answer because it solved my issue completely. i am going to give you a plus one though – ngreenwood6 Nov 22 '09 at 06:37
4

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
}
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
0

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!

Sablefoste
  • 4,032
  • 3
  • 37
  • 58
0

Along with jerjer answer(top), sometimes in your parent window and child window are not both external or both internal you will see a problem of opener undefined, and you cannot access parent page properties, see window.opener is undefined on Internet Explorer

Community
  • 1
  • 1
yesh
  • 41
  • 2
-2

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';
        //}
    };
Aruna
  • 1,962
  • 4
  • 25
  • 50