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.
-
See http://stackoverflow.com/questions/1112340/how-to-run-function-of-parent-window-when-child-window-closes – Crescent Fresh Nov 22 '09 at 05:10
7 Answers
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(){
}

- 15,371
- 2
- 44
- 78

- 8,694
- 30
- 36
-
1+1, helped me at the right time. Writing window.somefunction = function{ .... } is same as writing function somefunction() { .... } – Anuj Balan May 17 '13 at 04:48
-
1This doesnt work on IE if the the spawned window and the parent window are not from the same domain – Binod Kalathil Dec 19 '13 at 16:29
-
Thanks for great reply. But this code is not working on IE. How this works on IE browser. – Gaurav Gupta Aug 20 '14 at 09:23
-
Thank you, this is exactly what I've been looking for. It worked on both FF and IE for me. – user752746 Apr 19 '16 at 23:25
-
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.
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();
};

- 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
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
}

- 90,375
- 31
- 153
- 217
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!

- 4,032
- 3
- 37
- 58
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
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';
//}
};

- 1,962
- 4
- 25
- 50