Javascript - Notifying a Parent Window when a child window is closed or terminated
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("Child window seems to be closed!");
}
}
function childWindowUnloadNotification()
{
// Here we get notification from child window. Here we can decide if the 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>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 child window<br />
</body>
</html>
Advantages:
- Do not use polling.
- Recognize if child window is closed or is been updated by user .
- Found on IE and Mozilla Firefox.
Thanks & Regards,
Arun Manglick
SMTS || Microsoft Technology Practice - Bridgestone -
Note :
There's a catch in this. If you are using any pop-up blockers for the browser, then it might create problems. I was using Google Toolbar(which had the Popup-blocker in it) and it was not allowing the Notification to be send the parent!
No comments:
Post a Comment