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
2009-11-22 05:27:22
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
2009-11-22 05:37:43
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
2009-11-22 05:41: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
2009-11-22 05:44:32
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
2009-11-22 05:52:22
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
2009-11-22 06:37:21
+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
2009-11-22 06:15:41