Javascript parent and child window functions
- by Mike Thornley
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Lab 9-3</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
function myFunction(){
myWin = open("","","width=200,height=200");
with(myWin.document){
open();
write("<HTML><HEAD><TITLE>Child Window</TITLE>");
write("<SCRIPT>function myTest(){");
write("alert('This function is defined in the child window ");
write("and is called from the parent window.'); this.focus();}");
write("</SCRIPT></HEAD><BODY><H3>Child Window</H3><HR>");
write("<FORM><INPUT TYPE='button' VALUE='parent window function' ");
// Use opener property
write("onClick='opener.winFunction();'>");
write("<P><INPUT TYPE='button' VALUE='close window' ");
write("onClick='window.close();'>");
write("</FORM></BODY></HTML>");
close();
}
}
function winFunction(){
alert("This function is defined in the parent window\n" +
"and is called from the child window.");
myWin.focus();
}
//-->
</SCRIPT>
</HEAD>
<BODY>
<H3>CIW Web Languages</H3>
<HR>
<FORM NAME="myForm">
<INPUT TYPE="button" VALUE="open new window" onClick="myFunction();">
<!-- Invoke child window function -->
<input type="button" value="Click to open child window" onclick="javascript:void(myWin.myTest());"/>
</FORM>
<P>
</BODY>
</HTML>
To explain further what my initial query was, the code above, should open the child window (myWin) with the second button, the 'Open child window' button without the need to open the window with the first button or do anything else. It should simply call the myWin.myTest()function The child window will open when the second button is pressed but needs to have the child window open first (first button push) before it'll work. This is not the intended purpose, the 'Open child window' button should work without anything else needing to be done. For some reason the parent window isn't communicating with the myWin window and myTest fucntion. It's not homework, it's part of a certification course lab and is coded in the manner I have been shown to understand as correct. DTD isn't included as the focus is the JavaScript. I code correctly with regards to that and other W3C requirements.