View Full Version : Can a parent window know its child window closed?
balljilai
04-02-2004, 03:20 AM
For a parent window opened a child window, how can the parnet window be notified when the child window is closed or terminated?
Thank you. :thumbsup:
glenngv
04-02-2004, 04:45 AM
Supposed this is how the popup is opened:
win = window.open(...);
Then, you should repeatedly check if the popup is closed.
var timer, win;
function polling(){
if (win && win.closed) {
clearInterval(timer);
alert('popup window is closed.');
}
}
timer = setInterval('polling()',100);
balljilai
04-02-2004, 05:06 AM
Thank you. :thumbsup: :D
Roy Sinclair
04-02-2004, 05:40 AM
I suggest a less frequent polling speed. Do you really need to check ten times each second? Or would once every three to five seconds be plenty often enough?
While most CPUs these days are plenty fast enough to handle that kind of polling frequency it's a bad practice to use extra CPU just because you can and the occasional user running some CPU intensive background task will be happeir with you too.
sad69
04-02-2004, 07:33 AM
Could the child window not call a function of the parent window indicating that it's closing? Like onunload or something? Polling is only necessary if you've got events, right? Just a thought...
Sadiq.
glenngv
04-02-2004, 08:06 AM
onunload will not know the difference with page reload, navigating away from a link, changing the url in the address bar, and window closing.
Polling is only necessary if you've got events,right?
Wrong. :p :)
Polling is done if you events can't handle it. Like the way we did here, since onunload can't exactly determine if window is closing, polling is implemented.
liorean
04-02-2004, 08:12 AM
You can use window.onunload in the popup triggering a check for [object Window].closed in the main page. Tada, no polling needed!
sad69
04-02-2004, 08:14 AM
Polling is only necessary if you've got events,right?
I meant if you've got NO events... but whatever, I see your point, but liorean sees mine!
Sadiq.
glenngv
04-02-2004, 08:24 AM
Nice one liorean :thumbsup:
glenngv
04-02-2004, 08:34 AM
What if the user navigates away from the page by clicking on a link (if any) in the popup or by typing a new url in the address bar (even if address bar is turned off, it is still possible for IE to display it by pressing F11), and then by the time a new page is opened in the popup, the user closes the browser? That scenario would not notify the main page that the popup has closed. That issue is the very same thing that I raised earlier.
liorean
04-02-2004, 10:48 AM
Well, again, here you can let the unload event start a process in the main window. However, do avoid polling when it's not needed, only start polling for pages that doesn't know to notify the main window.
Roy Sinclair
04-02-2004, 03:28 PM
There are also conditions where the page can go away before it finishes running a "onunload" function so the calling page may not get it's notification.
Polling is ok but as I noted, it doesn't need to be done so frequently.
Krits0
07-14-2006, 08:24 PM
Hi for everybody!
I found this other solution to this problem that it seems very cool to me! :cool:
Advantages:
- Do not use polling.
- Recognize if child window is closed or is been updated by user .
- Found on IE and Mozilla Firefox.
PARENT WINDOW
<html>
<head>
<title>Main window</title>
<script type="text/javascript">
var childWindow = null;
function openChildWindow() {
childWindow = window.open('child.html','Childwindow','status=0,toolbar=0,menubar=0,resizable=0,scrollbars=1,top=50 ,left=50,height=375,width=650');
}
function checkChildWindowStatus() {
if (!childWindow || childWindow.closed) {
alert("Crazy flyer child window seems to be closed! :P");
}
}
function childWindowUnloadNotification() {
// Here we get notification from child window. Here we can decide if suck notification is
// raised because user close child window, or because user is playing with F5 key.
// NOTE: We can not trust on "onUnload" event of child window, because if user reload or refresh
// such window in fact he is not closing child. (However "onUnload" event is raised!)
setTimeout('checkChildWindowStatus()', 50);
}
</script>
</head>
<body>
Press bottom to open child window<br />
<input type="button" value="Open child window" onclick="javascript:openChildWindow();"/>
</body>
</html>
CHILD WINDOW
<html>
<head>
<title>Crazy flyer child window</title>
<script type="text/javascript">
function unloadNotification() {
// Raise unload notification to parent window
window.opener.childWindowUnloadNotification();
}
</script>
</head>
<body onunload="javascript:unloadNotification();">
Body of the creazy child window<br />
</body>
</html>
Best regards! :)
trusktr
10-04-2011, 08:47 AM
Sorry, I'm necroposting, but WOW... Krits0's post was very useful!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.