We help IT Professionals succeed at work.

Javascript test if child page is open will not work

Charlietn
Charlietn asked
on
Medium Priority
228 Views
Last Modified: 2012-05-12
I have been having this problem for a while and thought it was solved.  I am opening a child window that allows users to edit records.  When that window closes I need to run a function.  
This is the code I am spawning the child window with.  It fires the alert as soon as I click.  I thought it would alert when I close the child.
 
var child_win;
function cliedit()
{
var cs=document.getElementById("conno").value;
if (cs=="")
  {
  alert("No Client Selected");
  }
else
  {
 var cs=document.getElementById("conno").value;
 var urlstring="?cSearch="+cs;
 child_win=window.open("https://www.myvcaccount.com/cgi-bin/upclient.exe"+urlstring , "Client Data", "toolbar=no,menubar=no,width=800,height=425,resizable=no");
   if (child_win && child_win.closed);
    alert("window is closed")
  }
 }

Open in new window


Thanks
Comment
Watch Question

Top Expert 2013

Commented:
I suspect it is a timing issue.  Javasript does not wait for an instruction to complete before it starts the next statement.  The handle child_win may be created, but is not yet fully open so the alert fires.

You might want to use an onunload event in the child to trigger a function in the parent and let the parent know it is closing.

Author

Commented:
Would you happen to have an example of the onunload event?  I tried from some (actually your)post here and was not able to make it work.

Thanks
Top Expert 2013
Commented:
in the popup something like:

<body onunload="window.opener.alert('closing')">

Open in new window


probably gives you what you are looking for.

Author

Commented:
Gave me all I neede to think it through.  Good lesson for me.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.