List Info

Thread: Can a parent window know its child window closed?




Can a parent window know its child window closed?
user name
2006-07-14 19:26:16
Hi for everybody!
I found this solution to this problem that it seems very
cool to me and
I want to share!
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 CODE
--------------------------------------
<html>
<head>
<title>Main window</title>
<script type="text/javascript">
    var childWindow = null;
    function openChildWindow() {
        childWindow =
window.open('child.html','Childwindow','status=0,toolba
r=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 CODE
-----------------------------------
<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>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "JavaScript Forum" group.
To post to this group, send email to
JavaScript-Informationgooglegroups.com
To unsubscribe from this group, send email to
JavaScript-Information-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/JavaScript-Information
-~----------~----~----~----~------~----~------~--~---

[1]

about | contact  Other archives ( Real Estate discussion Medical topics )