BScript could assist
them in acquiring Web skills using familiar tools.
VBScript Object Model
VBScript has an object-oriented syntax. You must learn the properties, methods, and events associated with objects to become effective with this kind of platform. The figure
"VBScript Object Model"
presents an overview of the 11 objects in the IE3 browser. The generic "object" deals with external objects, such as ActiveX controls, Java applets, and ActiveX components. VBScript controls a session by interacting with these objects.
The window object is at the top level. Documents and all other objects in the window's immediate scope, except for frames and scripts, have a one-to-one relationship with the window object. Frames sit in windows but are themselves windows. Any one window can have multiple frames. Your VBScript code sits on a Web page in a script object.
Object events offer hooks that let you invoke scripts. For example, you use the
onLoad
window even
t to perform functions that need to take place when a window opens. The corresponding
onUnload
event lets you execute cleanup functions before a page exits a browser.
A document object's properties and methods pertain to the current Web page. For example, the bgColor and fgColor properties let you read and change the background and foreground colors of the current page. You must use prefix references for documents (and for all other objects, except windows) in VBScript. Document methods, such as
write
, must be prefaced with their antecedent (e.g.,
document.write string
writes the contents of
string
to the screen).
Forms, links, anchors, and general objects that are outside the scope of IE3 reside in documents. Each document can have many of these. Each form, in turn, can have more than one element, such as a button. Scripts reference most of these objects through their name attribute. For instance,
Document.NameOK
refers to a form that has a name attribute
of NameOK. When dealing with ActiveX controls, Java applets, and ActiveX components, you instead use the id attribute to reference these objects.
VBScript Programming Techniques
The
"Code Exhibit"
section presents a series of four examples that illustrate object-oriented style with VBScript. Visual Basic developers will instantly notice the resemblance between VBScript and their current development environment.
I start with the classic "Hello World." The document's write method normally inserts a string on the page. In this instance, its argument is an expression based on the
For
loop's index. Each pass through the loop evaluates the expression for a different index that sets the font tag's value. The
writeln
method on the loop's second line includes the "Hello World" message. This method causes the line to advance after writing to the screen. You must surround your code with the tags
<PRE>...</PRE>
to have preformatted t
ext displayed "as is" for the line-advancement feature to work.
The second example shows how to use buttons to control hyperlinking. Several lines at the bottom of the fragment create the button. The button appears on the screen with the caption "Go to Byte." The button's name attribute is
button1
. I use an event procedure to invoke the hyperlink. A two-part name identifies this kind of procedure:
objectname_eventname
.
When viewers click on the button, they generate the
onclick
event. This in turn launches the
button1_onclick
procedure. The location object in this procedure represents the current uniform resource locator (URL). The assignment line reevaluates the
href
property for the location object so that it launches the jump to the URL (in this case, the BYTE Site). The assignment feature lets you dynamically change hyperlinks in response to user choices.
The third code segment demonstrates how to clean data locally through a client-side script. Thi
s can reduce bandwidth use and improve performance. A user enters his or her name in a text box and then clicks on a button to submit the form. In a real application, the form object would have action and method attribute settings that describe its function. The button's type attribute is not
submit
, but
button
. If I used a type of
submit
, the form would go to the server without the opportunity for it to be intercepted with a local procedure. The event procedure
cmdSubmit_onclick
reminds the user to enter a value into the name field if he or she clicks on the button without entering one. If the data is valid, it passes it on the server with the form's
submit
method.
My final example illustrates the building of animated Web pages with VBScript. The script toggles two images. One image shows "Byte" in white letters on a black background. The other presents "Gotta have it" in black letters on the Web page's background. VBScript accomplishes this feat with the aid
of the Microsoft label and timer ActiveX controls. Both controls are available free at the ActiveX Web gallery (
http://www.microsoft.com/activex/gallery/
).