Working with Other WindowsWhen you open new windows, a window object is returned by the open method. When you have such a window object, you can access the document inside the new window with an expression such as windowObject.document . For example, here's how to access a text field in another window: windowObject.document.form1.text1.value . In this way, you can work with multiple windows in your code. Here's an example where you can open a new window with a button, enter text in a text field in the new window, and click a button (labeled "Get text from new window") to read the text from the text field, displaying it in the main window: (Listing 08-05.html on the web site)<HTML> <HEAD> <TITLE>Working With Multiple Windows</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- var window1 function openWindow() { window1 = window.open("08-06.html","window1","HEIGHT=300, WIDTH=300") } function getText() { if (window1 && !window1.closed) { document.form1.text1.value = window1.document.form1.text1.value } } function closeWindow() { if (window1 && !window1.closed) { window1.close() } } // --> </SCRIPT> </HEAD> <BODY> <H1>Working With Multiple Windows</H1> <FORM NAME="form1"> <INPUT TYPE="BUTTON" VALUE="Open new window" ONCLICK="openWindow()"> <INPUT TYPE="BUTTON" VALUE="Get text from new window" ONCLICK="getText()"> <INPUT TYPE="BUTTON" VALUE="Close new window" ONCLICK="closeWindow()"> <BR> <INPUT TYPE="TEXT" NAME="text1"> </FORM> </BODY> </HTML> And here's the HTML for the new window: (Listing 08-06.html on the web site)<HTML> <HEAD> <TITLE>A New Window</TITLE> </HEAD> <BODY> <H1>A New Window</H1> <FORM NAME="form1"> Enter some text: <INPUT TYPE="TEXT" NAME="text1"> </FORM> </BODY> </HTML> You can see the results in Figure 8.7, where I've entered text in the new window, and read it from the main window. Figure 8.7. Working with multiple windows.
|