
Visual Basic for Applications Unleashed

Chapter 21Web Page Programming: ActiveX and VBScript
The Scripting Object Hierarchy
Although it's mildly interesting to be able to make a Web page display a dialog box, the true fun occurs when you use VBScript to manipulate not only the various objects in the page, but also the browser itself! To accomplish all this, your VBScript code must interact with the scripting object hierarchy. This is an object model that defines the various items that VBScript can work with, as well as their properties, methods, and events.
Unlike the Office applications, which have dozens of objects to deal with, the scripting object model is relatively simple. It begins with a single top-level object called the Window object. Under the Window object are just six second-level objects:
- Document: Represents the current document in the window.
- Location: Represents the URL of the current document.
- History: Represents the history list for the browser.
- Frame: Represents an array of the frames in the current window.
- Script: Represents the script being used.
- Navigator: Represents the browser application (such as Internet Explorer) in which the Web page is being displayed.
The next few sections describe a few of these objects, as well as their most useful properties and methods.
The Window Object
The Window object represents the browser window and therefore serves as a container for the entire scripting object model. Note that VBScript assumes that the Window object is being referenced by default. Therefore, you rarely have to mention the Window object directly. For example, the statements Window.name and name are equivalent. This section examines some common properties and methods for this object.
Window Object Properties
The Window object properties govern various aspects of the window's appearance and how the window fits into the current frame setup:
- defaultStatus: This property determines the default text that appears in the browser's status bar (that is, the text that appears when the browser is waiting for input).
- name: This property returns the name of the window. You usually name a window while using frames (that is, by using the NAME attribute in the <FRAME> tag). You can also give a window a name by using the open method (see the next section) or by using the TARGET attribute in the <A HREF> tag.
- opener: Returns a Window object that represents the window that opened the current window.
- parent: Returns a Window object that represents the parent window of the current window. (The parent is the frame in which the current window resides.)
- self: Returns a Window object that represents the current window.
- status: Sets the text in the browser's status bar.
- top: Returns a Window object that represents the topmost window in the current frame array.
Window Object Methods
You can use the Window object's methods to manipulate the browser window:
- alert: This method displays an alert box. This is similar to running MsgBox in VBA with the vbExclamation constant.
- close: Closes the window.
- confirm: This methods displays a dialog box with a message and OK and Cancel buttons (it's similar to using MsgBox in VBA with vbOKCancel):
- return = confirm("Do you want to submit the form?")
If the user clicks OK, confirm returns True; if the user clicks Cancel, confirm returns False.
- navigate: Navigates the window to a specified URL. For example, the following statement navigates to http://www.mcp.com/:
- window.navigate "http://www.mcp.com/"
- open: Opens a new window using the following syntax:
-
window.open(url, target, options)
| url |
A string specifying the URL to display in the new window. |
| target |
The target name to use for the new window. |
| options |
A comma-separated list of values that determine various properties of the new window:
| toolbar |
A Boolean value (1 or 0, yes or no) that determines whether the new window displays the toolbar. |
| location |
A Boolean value (1 or 0, yes or no) that determines whether the new window displays the location box. |
| status |
A Boolean value (1 or 0, yes or no) that determines whether the new window displays the status bar. |
| menubar |
A Boolean value (1 or 0, yes or no) that determines whether the new window displays the menu bar. |
| scrollbars |
A Boolean value (1 or 0, yes or no) that determines whether the new window displays the scrollbars. |
| resizeable |
A Boolean value (1 or 0, yes or no) that determines whether the new window can be resized. |
| width |
Sets the width of the new window in pixels. |
| height |
Sets the height of the new window in pixels. |
| top |
Sets the Y-coordinate position of the top of the window. |
| left |
Sets the X-coordinate position of the left edge of the window. |
|
Here's an example:
window.open "http://www.yahoo.com/", "Frame2", "toolbar=no, location=no"
- prompt: Displays a dialog box to ask the user for input (similar to VBA's InputBox function). Here's the syntax:
-
prompt(prompt, default)
| prompt |
The prompt that appears in the dialog box. |
| default |
The initial value that appears inside the text box. |
Window Object Events
The Window object supports only two events:
- onLoad: This event fires when the contents of the window are loaded.
- onUnload: This event fires when the contents of the window are unloaded.
In both cases, you define the event handler within the <BODY> tag, like so:
<BODY onLoad="HiThere()" onUnload="Bye()">
<SCRIPT LANGUAGE="VBScript">
Sub HiThere()
MsgBox "Welcome!"
End Sub
Sub Bye()
MsgBox "So long!"
End Sub
</SCRIPT>
The Document Object
The Document object represents the Web page currently loaded into the browser. It also serves as a container for the objects inside the page, including the Links object (the collection of all the Link objects on the page), the Forms object (the collection of Form objects on the page), and the Location object.
Note that, unlike the Windows object, to work with the Document object properties and methods, you must precede the property with the document keyword (for example, document.title). The next two sections take you through some Document object properties and methods.
Document Object Properties
The Document object's properties let you customize the document display programmatically. This includes the background color, the link colors, the title of the document, and more.
- anchors: Returns the Anchors objectthe collection of all the anchors in the document. (Use document.anchors.length to determine the number of anchors in the document.)
- bgColor: Returns or sets the color of the document's background. When setting this property (and any of the other color-related properties), you use a string value that consists of either a color name or an RGB value. For example, both of the following properties set the background color to white:
- document.bgColor = "white"
- document.bgColor = "#FFFFFF"
- fgColor: Returns or sets the color of the document foreground (text).
- forms: Returns the Forms objectthe collection of all the forms in the document. For detailed information about the Forms object, see "How VBScript Interacts with Forms" later in this chapter.
- lastModified: Returns a date string that corresponds to the date the document was last modified.
- linkColor: Returns or sets the color of the document's links.
- links: Returns the Links objectthe collection of all the links in the document. Each member of this collection is a Link object, which has various properties that govern each link's URL (such as href, protocol, and host).
- location: Returns the Location object. The properties of this object (such as protocol, host, and pathname) specify the various parts of the document's URL.
- title: Returns the document's title (that is, the text between the <TITLE> and </TITLE> tags).
- vLinkColor: Returns or sets the color of the document's visited links.
Document Object Methods
The Document object has only a few methods, and only one of them is particularly useful:
- write: This method writes text into the document:
- document.write text
Here, text is the string you want to insert. Here are a few things to bear in mind when using this handy method:
- The text is written at the point where the write method resides in the document.
- The write statement must be executed when the browser parses the script. Therefore, you insert the write method either by itself, outside of a procedure (yet still within the <SCRIPT> container, of course), or inside a procedure that is invoked by the Call statement.
- You can include HTML tags in the text argument. The browser will render them faithfully.
Listing 21.3 shows several examples of the write method (see WRITE.HTM on the CD).
Listing 21.3. Some examples of the write method.
<HTML>
<HEAD>
<TITLE>Document.Write Examples</TITLE>
</HEAD>
<BODY BGCOLOR=WHITE>
This line is part of the regular body text.
<P>
<SCRIPT LANGUAGE="VBScript">
' This line runs when the browser parses the script
Call WriteGreeting
Sub WriteGreeting
Dim hr
hr = Hour(Now)
If hr < 12 Then
document.write "Good morning!"
ElseIf hr < 17 Then
document.write "Good afternoon!"
Else
document.write "Good evening!"
End If
End Sub
</SCRIPT>
<P>
Another line of body text.
<P>
<SCRIPT LANGUAGE="VBScript">
' These statements are executed at parse-time
document.write "You're visiting at "
document.write Time() & " on " & Date() & "."
</SCRIPT>
</BODY>
</HTML>
The first <SCRIPT> container uses a Call function to execute the WriteGreeting procedure during parse time. WriteGreeting checks the current hour and displays an appropriate greeting. The second <SCRIPT> container just has a couple of write methods. Again, these are executed at parse time. As you can see in Figure 21.16, the text specified by the write methods is displayed where the write statements occur in the HTML file.

Figure 21.16.
The write method inserts text into a Web page.
|
NOTE: THE WRITELN METHOD
|
|
The Document object also has a writeLn method, which is identical to write, except that it includes a newline character at the end of the specified text. However, most browsers ignore newline characters, so this method works only if you precede it with a <PRE> tag.
|
The Navigator Object
The Navigator object provides you with information about which browser is being used to display the window. The Navigator object has four properties:
- appCodeName: This property returns the browser's code name (such as "Mozilla").
- appName: This property returns the browser's application name (such as "Microsoft Internet Explorer").
- appVersion: This property returns the browser's version number. Note that this usually contains the operating system as well:
2.0 (compatible; MSIE 3.0A; Windows 95)
- userAgent: This property returns the browser's user agent (usually the appCodeName combined with the appVersion).
Copyright © 1995-2008 Paul McFedries and Logophilia Limited
|