Opening and Closing New WindowsThe window. open method is a big one, because it enables you to open new browser windows. Here's how you use this method: window.open( [ URL ] [, name ] [, features ] [, replace ]) This method returns the new window object. The arguments for this method are URL and name . The URL argument specifies the URL of the document to display. If no URL is specified and you pass an empty string ( "" ), a new window with the title about:blank displays. The name argument is a string that specifies the name of the window. (The name is a one-word name for the NAME attributenot the title of the windowthis name is used as the value for the TARGET attribute on a form or an element.) You also can use these values here:
The features argument is a string list of items separated by commas. Each item consists of an option and a value, separated by an equals sign (for example, "fullscreen=yes , toolbar=yes" , as we'll see in a page or twonote that you should not use spaces between these items, because NS4 can't handle such spaces). You can find the possible values in Table 8.5all values are Boolean values you set to "yes," "no," 1 (same as yes), or 0 (same as no) unless otherwise noted. Table 8.5. The Features of Windows Opened with the window.open Method
Tip If you display a new window in full-screen mode, the browser's controls won't be visible, so you should provide a way for the user to close the browser, or at least let the users know they can close the window with Ctrl+W in both the Internet Explorer and the Netscape Navigator. Finally, the replace argument is a Boolean value that holds true if the new document replaces the current document in the history list or false if the new document should create a new entry in the history list. We've already seen the open method at work in Chapter 7, "Using window and frame Properties;" in this example (it's Listing 07-02.html on the web site), we opened a new blank window, creating the window object window1 , and then wrote to the new window with the window1.docuent.write method like this (note that the only features we're setting here are the height and width: "height=300,width=300" , but we could have done more like this "height=300,width=300,scrollbars=yes,toolbar =no" and so on): <HTML> <HEAD> <TITLE>Using the closed and opener Properties</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- var window1 function openWindow() { window1 = window.open("","window1", "height=300,width=300") window1.document.write("<HTML><BODY><H1>A New Window</H1><BR>" + "<FORM><INPUT TYPE='button' VALUE='Close the original window'" + "ONCLICK='window.opener.close()'>" + "</FORM></BODY></HTML>") window1.document.close() } function closeWindow() { if (window1 && !window1.closed) { window1.close() } } // --> </SCRIPT> </HEAD> <BODY> <H1>Using the closed and opener Properties</H1> <FORM> <INPUT TYPE="button" VALUE="Open New Window" ONCLICK="openWindow()"> <INPUT TYPE="button" VALUE="Close New Window" ONCLICK="closeWindow()"> </FORM> </BODY> </HTML> You can see the results in Chapter 7, in Figure 7.2. Tip When you're opening new windows and writing HTML to them, as we are here, it's sometimes wise to wait for a while after opening a new window to make sure it's ready to go (that is, fully loaded) before writing to it ( especially with the Internet Explorer). You can do that with the window method setTimeout , discussed in the previous topic. |