Приглашаем посетить
Экономика (economics.niv.ru)

Modifying the HTML Stream

Modifying the HTML Stream

Originally, the object model in Internet Explorer 3.0 did not allow the document contents to be changed once the page was rendered. Since then, a number of methods have been exposed for outputting HTML into the document's stream while the page is being loaded or for generating an entirely new HTML document in another frame or browser instance. The example earlier in this chapter that outputs the source file's last modified date illustrates how to add HTML to the stream of a downloading document. The example in Chapter 5, "Window and Frame Management," that creates a diagram of a document's frameset hierarchy shows how to generate complete documents.

The available methods for adding contents to an HTML stream as it is being parsed and for generating complete documents are as follows:

Even though the object model now provides access to the stream, these methods are still very useful for generating contents as the page loads. Your scripts can generate different HTML code in response to the different conditions they encounter.


NOTE: One of the innovations in Dynamic HTML is the ability to modify the document's contents after the page is loaded. You can modify a document using properties and methods of the Body element and its child elements, or using a new object named TextRange. Modifying the contents of the document is discussed in detail in Part IV.

Marked Sections

If you are familiar with SGML marked sections, you will see that scripts that write to the HTML stream are similar to marked sections. Marked sections allow a browser to use different contents, depending on a specific condition. For example, a Java applet, a plug-in, or maybe an image can be output to the screen, depending on the feature support of the browser. However, generating contents through scripts has a significant disadvantage in that the contents of the document cannot be predetermined and indexed using tools without evaluating the scripts on the page.

Writing HTML into the Stream

The write and writeln methods allow you to write HTML into the current stream while the document is loading or into another stream that has been opened using the open method. The write method is used in quite a few examples in this book to output HTML into the stream while loading. Arguments passed to the write method are always converted to strings before they are output into the document.

The writeln method is similar to the write method, but it appends an end-of-line character to the end of the line. Whether you use write or writeln rarely matters because most end-of-line characters are ignored in the HTML stream. End-of-line characters and spaces are important in only three instances:

You should not use the write and writeln methods on the current document after the document has finished loading unless you first call the open method, which clears the current document's window and erases all variables.

Creating Documents Using the open and close Methods

The document methods open and close allow you to create new documents in other frames or windows. These documents do not even have to be written in HTML because the open method takes a MIME-type identifier. Therefore, if you know the format for an image or other document type, the image or document type can be output directly into a window.

The following code demonstrates using the open and close methods to output document information for a specified window into another window:

function docInfo(win) {
   /* Create an About dialog box. */
   var aboutWindow = window.open("", "Info",
      "toolbar=no; location=no; directories=no; width=375; " +
      "height=250; status=no; menubar=no; resizable=no");
   var prop;
   // Open a stream on the new window.
   aboutWindow.document.open();

   // Output document information.
   aboutWindow.document.write("<H1>Document Information</H1>");
   for (prop in win.location) 
      aboutWindow.document.write(prop + ": " + self.location[prop] + 
         "<BR>");
   // Close the stream on the new window.
   aboutWindow.document.close();
} 


NOTE: The clear method was exposed in Internet Explorer 3.0 for clearing the document's contents. This method should no longer be used because its future support is questionable and it acts unpredictably on different browsers. Instead, the open and close methods are sufficient for clearing and generating new documents.

Writing Scripts into the Stream

Your script can insert additional scripts into the stream. When you use this technique, be careful how your script closes the Script element it is inserting. Your script must insert the </SCRIPT> tag as two strings to be concatenated, as shown in the following code. Otherwise, the HTML parser will assume that the tag ends the script you are writing rather than the script it is inserting.

<SCRIPT LANGUAGE="JavaScript">
   // Example of dynamically generating a script
   document.write("<SCRIPT LANGUAGE=`JavaScript'> x = 0; <" +
      "/SCRIPT>");
</SCRIPT> 

[Содержание]