Working with Windows and Frames
Sunday, February 8th, 2009This tutorial explores the use of windows and frames with JavaScript. We will learn about the properties of the window object, how to create status bar messages, and how to work with the location and history objects. Next, the text shows us how to use JavaScript to create pop-up windows, how to design Web pages that work properly in the presence of pop-up blockers, and how to create alert, confirm and dialog boxes. Finally, we learn to work with frames and framesets using JavaScript. I’ve recapped each of this tutorial’s sessions in the next three paragraphs.
In the first session, we learn about the window object, status bar messages and the location and history objects. We’re next taught how to create permanent and transient status bar messages. We’ll also learn how to work with the properties of the location and history objects. Finally, we will be able to apply automatic page navigation to a Web site.
The permanent status bar message is only permanent for the current document. Once the browser loads a different page, the permanent status bar message returns to either the browser default or a permanent message specified for the new page. You have to instruct the browser to stop displaying a transient status bar message. You can do so by using the onmouseout event of the <a> tag.
JavaScript can use the history object to navigate through the history list, by going forward or back. If a user is at the beginning or at the end of the history list, the back() or forward() method, respectively, has no effect, and the current page remains in the browser. To navigate to a particular page in the history object use the go(integer) method.
In many situations it is useful to redirect the browser to another page. The Reference Window on page 700 describes two ways to automatically redirect Web pages. If you set the wait time before redirection to zero, the redirection to occur almost instantaneously and often users are not even aware that a redirection has taken place.In the second session, we will walk through working with pop-up windows, alert, confirm and prompt dialog boxes. After that you’ll study modal and modeless windows. Walk through the code so you learn how to work with pop-up windows with JavaScript so you learn how to adjust your code to accommodate pop-up blockers and how to work with the properties and methods of pop-up windows. After that, we’ll cover creating alert, confirm, and prompt dialog boxes. In fact, the tutorial shows us how to write content directly into a pop-up window. Finally, we’ll look at how to work with modal and modeless windows. You should remember these from VB as they are similar in function (but not in coding).
You can create new windows using both HTML and JavaScript. You can create secondary windows with HTML code, by using the target property of the <a> tag. You can give a new window any name, except one that is reserved by HTML for other purposes. If a window with the same name were already open, its contents would be replaced with the new page. If the name you select is one of the names of your frames, the page will be loaded in that frame and not in a new window.
JavaScript lets you have more control on the windows you create. For example, you can specify the window size, its position on the screen, and whether the new window has toolbars, a menu bar, and a status bar. The Reference Window on page 703 describes how to create a pop-up window using JavaScript. Figure 13-12 describes the different window features and their values. In JavaScript, if you don’t specify a features list, the new window adopts the characteristics of the browser window that created it (its parent). It’s important to point out that if you do specify a features list, then some rules apply: if you don’t specify a width or height, the width or height of the original browser window is used; and, if you don’t include a particular feature in the list, that feature will not appear in the window. By default a pop-up window will appear in the upper-left corner of your screen; however you can specify a different location using the features named left and top in Internet Explorer. The Reference Window on page 707 describes how to control the pop-up window features. Don’t spend much time studying variations in Netscape since this browser is no longer supported by its maker.
You can write JavaScript code to control the interaction between various browser windows. As you would expect, you can do lots of things with browser windows, like specifying which window has the focus, allowing users to move windows to different locations and resize them, and automatically close windows. Figure 13-17 describes some of the methods of the window object.
Dialog boxes and windows can either be modal or modeless. A modal window prevents users from doing work in any other window or dialog box until the window is closed. An example of a modal window is an alert box in a browser window. A modeless window allows users to work in other dialog boxes and windows even as the window stays open. For example, pop-up windows are general modeless. You can force a pop-up window to be modal by adding the following event handler to the window’s body element: onblur = “self.focus()”.
Figure 13-26 describes some of the features of modal and modeless dialog boxes and their values. Unfortunately, you can’t use the document.write() method to write content directly to a new window created with showModalDialog() nor with showModelessDialgo(). Any information must be sent through the data in the arguments parameter for the method being used. The value(s) can be later accessed within the pop-up window using the dialogArguments property of the window object.
In the third session, we finally work with frame and frameset objects and study how to navigate between frames and learn how to change the content of a frame. Next, we will see how to change a frame layout and how to block frames and force pages into framesets. The last piece of coding in this tutorial revolves around learning about how to work with inline frames.
As you probably know, frames are organized into framesets, each frame displaying a separate URL. A window can contain multiple framesets. Framesets can be nested inside each other. The name attribute of a frame element is used when creating links whose targets are designed to appear in specific frames. The Reference Window on page 728 describes how to reference frames and framesets.
To navigate between frames you have to navigate through the hierarchy of frames in the frameset. You can use the parent and top keywords to navigate between frames.
In most cases JavaScript treats a frame as a separate browser window, so most of the properties and methods that can be applied to window objects can also be applied to frame objects. Check out page 730 for an example about how to change the content of a frame. Keep in mind that this is also how you change the contents of a window object.
A frameset is laid out in rows or columns. You can use the rows and columns properties of the frameset object using JavaScript. A frameset can be removed of hidden by setting its height (or width) value to 0 pixels.