|
Free Open Book
Sams Teach Yourself JavaScript in 24 Hours |
Controlling Windows with ObjectsIn Hour 4, "Working with the Document Object Model (DOM)," you learned that you can use DOM objects to represent various parts of the browser window and the current HTML document. You also learned that the history, document, and location objects are all children of the window object. In this hour, you'll take a closer look at the window object itself. As you've probably guessed by now, this means you'll be dealing with browser windows. A variation of the window object also enables you to work with frames, as you'll see later in this hour. The window object always refers to the current window (the one containing the script). The self keyword is also a synonym for the current window. As you'll learn in the next sections, you can have more than one window on the screen at the same time, and can refer to them with different names. Properties of the window ObjectAlthough there is normally a single window object, there might be more than one if you are using pop-up windows or frames. As you learned in Hour 4, the document, history, and location objects are properties (or children) of the window object. In addition to these, each window object has the following properties:
By the Way The properties of the window.screen object include height, width, availHeight, and availWidth (the available height and width rather than total), and colorDepth, which indicates the color support of the monitor: 8 for 8-bit color, 32 for 32-bit color, and so on. Creating a New WindowOne of the most convenient uses for the window object is to create a new window. You can do this to display a documentfor example, a pop-up advertisement or the instructions for a gamewithout clearing the current window. You can also create windows for specific purposes, such as navigation windows. You can create a new browser window with the window.open() method. A typical statement to open a new window looks like this: WinObj=window.open("URL", "WindowName", "Feature List");
The following are the components of the window.open() statement:
The features available in the third parameter of the window.open() method include width and height, to set the size of the window in pixels, and several features that can be set to either yes (1) or no (0): toolbar, location, directories, status, menubar, scrollbars, and resizable. You can list only the features you want to change from the default. This example creates a small window with no toolbar or status line: SmallWin = window.open("","small","width=100,height=120,toolbar=0,status=0");
Opening and Closing WindowsOf course, you can close windows as well. The window.close() method closes a window. Browsers don't normally allow you to close the main browser window without the user's permission; this method's main purpose is for closing windows you have created. For example, this statement closes a window called updatewindow: updatewindow.close(); As another example, Listing 10.1 shows an HTML document that enables you to open a small new window by pressing a button. You can then press another button to close the new window. The third button attempts to close the current window. Depending on your browser and its settings, this might or might not work. If it does close the window, most browsers will ask for confirmation first. Listing 10.1. An HTML Document That Uses JavaScript to Enable You to Create and Close Windows
This example uses simple event handlers to do its work, one for each of the buttons. Figure 10.1 shows Firefox's display of this page, with the small new window on top. Figure 10.1. A new browser window opened with JavaScript.
|
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |