A Document Object Example

The Document object allows access to the currently loaded web page. The properties of the Document object can be accessed through VBScript to affect the appearance of the web page. The example shown in Listing 3-4 changes the heading based on the current month. Figure 3-9 shows a sample of the output.

<HTML>
<HEAD>
<TITLE>Immediate Execution</TITLE>
<!—
This demo uses the Write method of the
Document object to create dynamic HTML code.
The web page will display a different
heading, based on the current month.
—>

<SCRIPT LANGUAGE="VBScript">
<!—
    Hide code from old browsers
    Dim strSpecial
    If Month(Now)=1 Then strSpecial="Steaks"
    If Month(Now)=2 Then strSpecial="Chicken"
    If Month(Now)=3 Then strSpecial="Hot Dogs"
    If Month(Now)=4 Then strSpecial="Hamburgers"
    If Month(Now)=5 Then strSpecial="Porterhouse"
    If Month(Now)=6 Then strSpecial="Filet Mignon"
    If Month(Now)=7 Then strSpecial="NY Strip"
    If Month(Now)=8 Then strSpecial="Lamb"
    If Month(Now)=9 Then strSpecial="Veal"
    If Month(Now)=10 Then strSpecial="Pork Chops"
    If Month(Now)=11 Then strSpecial="Ham"
    If Month(Now)=12 Then strSpecial="Turkey"
    Document.Write "<CENTER><H2>" & strSpecial & _
    "</H2></CENTER>"
—>
</SCRIPT>

</HEAD>
<BODY BGCOLOR=#FFFFFF>
<H2><CENTER>Check out our special this month!<CENTER></H2>
<P>
<P>
</BODY>
</HTML> 

Listing 3-4.

The Document object in VBScript.

Figure 3-9.

Sample output using the Document object.

Although this example shows other uses, the most common use of the Document object is to reference forms contained inside the document. These forms are always referenced by the syntax Document.formname. With this reference, you can access any control on any form.

© 1996 by Scot Hillier. All rights reserved.