The Document Object

The Document object represents the current HTML page in the browser. The Document object also represents any object on the HTML page, such as a link, a form, a button, or an ActiveX object. The properties and methods for the Document object must be fully qualified when used. The Document object supports many properties and methods that modify the HTML page. These properties and methods, such as LinkColor and Write, must be used inline

when the HTML page is loading. These properties and methods cannot be called after the HTML page is loaded because the Internet Explorer parses the HTML only during page load. VBScript code can be executed inline as the load occurs. The following code shows an example where text is being written to an HTML page and the background color is being set to white:

<SCRIPT LANGUAGE="VBScript">
    Document.Write "<H2>Inline HTML modification</H2>"
    Document.BGColor="#FFFFFF"
</SCRIPT> 

Executing inline code to modify an HTML page while it loads is known as immediate execution. Immediate execution is a valuable technique because it allows you to create web pages that are different each time they are loaded. As a simple example, the following code displays the date of the last update for the web page:

Document.Write "<P><CENTER>"
Document.Write "Page last updated"
Document.Write Document.LastModified
Document.Write "</CENTER><P>" 

This technique can be expanded to display new pictures or text on the basis of the current date, or even randomly.

Tables 3-6 and 3-7 describe the properties and methods of the Document object.

Table 3-6.Document Object Properties

Property Type Description Usage
LinkColor String Returns or sets the link color. Accepts a string input representing the hexadecimal color value or a specific color command Document.LinkColor= "#FF0000" Document.LinkColor= "Red"
ALinkColor String Returns or sets the anchor color. Accepts a string input representing the hexadecimal color value or a specific color command. Document.ALinkColor="#FF0000" Document.ALinkColor="Red"
VLinkColor String Returns or sets the visited link color. Accepts a string input representing the hexadecimal color value or a specific color command. Document.VLinkColor="#FF0000" Document.VLinkColor="Red"
BGColor String Returns or sets the background color. Accepts a string input representing the hexadecimal color value or a specific color command. Document.BGColor="#FF0000" Document.BGColor="Red"
FGColor String Returns or sets the foreground color. Accepts a string input representing the hexadecimal color value or a specific color command. Document.FGColor = "#FF0000" Document.FGColor = "Red"
Anchors Object Returns an Anchors array. Set MyObject= Document.Anchors(1)
Links Object Returns a Links array. Set MyObject= Document.Links(1)
Forms Object Returns a Forms array. Set MyObject= Document.Forms(1)
Location String Returns a string representing the complete URL for the current document. MyVar= Document.Location
LastModified String Returns a string representing the date when the current page was last modified. MyVar= Document.LastModified
Title String Returns the read-only title of the current HTML page. MyVar= Document.Title
Cookie String Returns or sets the cookie for the current document. MyVar= Document.Cookie
Referrer String Returns the URL of the referring document. MyVar= Document.Referrer

Table 3-7.Document Object Methods

Method Return Type Description Usage
Write None Writes the given string into the current document. Document.Write "Hello"
WriteLn None Writes the given string into the current document with the addition of a newline character at the end. (This works only if the scripting section is surrounded by <PRE></PRE> tags.) Document.WriteLn "Hello"
Open None Opens the document stream for output. Document.Open
Close None Updates the screen to display all the strings written after the last Open method call. Document.Close
Clear None Closes the document output stream and writes the data to the screen. Document.Clear

© 1996 by Scot Hillier. All rights reserved.

Show: