Chapter 27
Windows
What are Windows?
When you load your browser application (Netscape Navigator or Microsoft Internet
Explorer), a window immediately appears. This window is known as a browser window,
or a window for short. Figure 27-1 shows a single image.
Figure
27-1. A standard browser window.
A browser window does not have to feature buttons, a location field, and so on. As a
matter of fact, a browser window can consist of only the content zone and a title bar, as
shown in Figure 27-2.
Figure
27-2. A browser window does not have to contain a status bar,
menus, and other navigation tools.
It is possible to have several browser windows simultaneously open. The simplest way to
open a window is by selecting New Web Browser from the File menu under Navigator,
or New Window under Internet Explorer. Since each window requires a reasonable
amount of RAM, there is a limit to the number of windows you can open.
Opening a Window with JavaScript
You can use JavaScript to open a window, as the result of a button click or any other
operations. Since each window is represented as a distinct window object, opening
a new window actually creates another window object. The new window is not
connected to the window that opened it in any hierarchical relation. You can access it,
though, by its predefined name.
You can open a new browser window with the window.open() method. Furthermore,
this method lets you specify the way the new window looks on the user’s computer screen.
The general syntax of the window.open() method is as follows:
[windowVar = ][window].open("URL", "windowName", ["windowFeatures"])
“windowVar = ” is required if you want to establish a connection between
the current window and the new one. You can use this variable to reference the window
object of the new window. Consider, for example, the following statement:
var homeWindow = open("http://www.geocities.com", "geo")
This statement creates a new window of the same dimensions and features as if it were
opened through the browser’s File menu. The initial document loaded into the new window
is http://www.geocities.com, but a new one can be loaded via a script in the
original window, using such a statement as:
homeWindow.location.href = "http://www.netscent.com/"
Since it is impossible to access a window’s properties without assigning its object
reference to a variable, it is a good practice to specify a variable for every window you
create via JavaScript. Also, in order to ensure control over the window throughout the
script, it is better to use a global variable rather than a local one. This is why some
JavaScript programmers never use the var keyword with the window.open()
method.
There are many reasons for a new window to fail during opening. Lack of resources and
closing by the user are only two possibilities. Before addressing any property, method, or
function of a window, you should test if that window exists by comparing the variable
holding its object reference to null. Here is a simple example:
var homeWindow = open("http://www.geocities.com", "geo")
if (homeWindow != null)
homeWindow.location.href = "http://www.netscent.com/"
The first argument accepted by the window.open() method is the URL of the
initial document to be loaded in the new window. Use an empty string if you do not want
any document to load in the new window. You might want, for example, to open a new window
and generate its HTML content via a data stream from the originating window. You would
then use the following syntax:
var win = open("", "myWindow")
if (win != null) {
win.document.open("text/html")
win.document.write("<H1>This is a new window!</H1><HR>")
win.document.close()
}
There are two different open() methods in this script. While the first one is
invoked as a method of the window object, the second open() is called as
a method of the document object, and is used to open a data stream to the new
window’s document. Figure 27-3 illustrates these two open() methods.
Figure
27-3. JavaScript features two different open() methods.
The window.open() method’s second argument, windowName, is the
window name to use in the TARGET attribute of the <FORM> or <A>
tag. This argument must be a string containing alphanumeric characters and underscores. Do
not confuse this value with the variable that is assigned the returned value by the
method. A window’s name does not play any practical role in terms of JavaScript, but
rather enables you to direct documents to that window in the same way you would target a
document to a frame. The variable that we assigned window.open() method to, on
the other hand, has a major role in JavaScript. It is the window object of the
new window, through which the window’s elements can be manipulated. Internet Explorer
3.0x does require a name (an empty one is OK) for every new window. You must use a
distinct name for each new window, or else the new window won’t open and the contents of
the old window bearing the same name will be replaced by the new window’s document.