views:

550

answers:

2

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.

+2  A: 

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
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
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
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
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
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
+1  A: 

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 no do this on the parent, otherwise opener.somefunction() will not work, doing window.somefunction makes somefunction as public:

function somefunction(){

}
jerjer
That was exactly what I was looking for works great.
ngreenwood6