Example 3: Generating an HTML File
The function we're going to write will show some information about the current HTML window. The remainder of this article focuses on the DisplayPage function.
Opening a Window
The open method opens a new window. The syntax is
[windowVar =] window.open("URL", "windowName", ["windowFeatures"])
By assigning the return value of this function to a variable, we can refer to the window's properties later on. For example, windowVar.status would return the contents of the new window's status bar. Here's the first command for the DisplayPage routine:
displayWindow = window.open("","WDWindow")
Leaving an empty value for URL gives us a blank window. Omitting the windowFeatures parameter gives us a normal browser window.
Exercise: Look up the details of the open command in Netscape's documentation. Rewrite the command above so that rather than a browser window, the user sees a blank window with no menus, toolbars, or buttons. The document Object and the Window Hierarchy So far we've had objects, properties, and methods that have been used in a fairly straightforward way.
is extended to:
We're zeroing in on the really good stuff here, the write method of the document object. By sending out text to a document, we can create HTML on the fly, providing dynamic content without having to scurry back to the server. The important thing to know about the write method is that it sends out text as a stream, and doesn't actually display the text until the document is closed with the close method. Here's code to generate a skeleton HTML page in the DisplayPage function.
displayWindow.document.write("Window summary information:<BR>")
displayWindow.document.write("The topmost window document is:")
displayWindow.document.write("The parent window document is:")
displayWindow.document.write("<STRONG>The current window document is:")
displayWindow.document.write("The URL for this document is:")
As you can guess from the syntax above, window.top refers to the topmost window object; window.parent refers to the parent window; window (or you can say, window.self) refers to the current window, that is, the window that contains the code that is currently running. Netscape's documentation of the window object covers these concepts in more detail.
Exercise: Try running the program from the tutorial menu (tutor.htm), then again after loading only ex3cont.htm into the browser window. In the latter case, the same window is parent, top, and self.
Part 2 ]
Part 1 > ]