Because VBScript is a subset of Visual Basic for Applications, many of the familiar features of Visual Basic for Applications are supported. However, certain key differences between the two programming environments require detailed examination. The major differences are covered here in alphabetical order.
VBScript provides no support for classes as they are used in Visual Basic 4.0. You cannot use <SCRIPT></SCRIPT> tags to create anything except reusable functions and subroutines. Development in VBScript is purely procedural. However, in the Internet Explorer 3.0, VBScript can automate any ActiveX component, including OLE servers (now called ActiveX components) created in Visual Basic 4.0.
VBScript does not support control arrays. (A control array is a group of duplicate controls sharing the same name.) This is unfortunate because grouping controls into arrays always simplifies coding. You can work around this limitation, however, by having controls call a centralized routine whenever their respective events are called. For example, the following HTML code shows three buttons, each of which calls the same subroutine. Note that an index is passed with the call. In the function, we can use a Select…Case statement to determine which button was pressed:
<SCRIPT LANGUAGE="VBScript">
Sub WhatButton(intIndex)
Select Case intIndex
Case 1
MsgBox "Button1!"
Case 2
MsgBox "Button2!"
Case 3
MsgBox "Button3!"
End Select
End Sub
</SCRIPT>
<INPUT TYPE="BUTTON" OnClick="WhatButton(1)">
<INPUT TYPE="BUTTON" OnClick="WhatButton(2)">
<INPUT TYPE="BUTTON" OnClick="WhatButton(3)">
Unfortunately, the above technique works only for controls defined with HTML. ActiveX controls downloaded into a web page do not support such events as OnClick. The events for each ActiveX control are defined as separate subroutines. Therefore, no workaround exists for control arrays in ActiveX components. We discuss events, functions, and subroutines in detail later in this chapter.
VBScript supports data access by using data access objects, as well as by using the Internet Server API (ISAPI). Data access objects (DAOs) and remote data objects (RDOs) can be inserted into an HTML page and automated. Data access to any ODBC data source from an HTML page can be accomplished via ISAPI, provided with the Internet Information Server (IIS). IIS is discussed in detail in Chapter 5.
VBScript does not provide any debugging features. There are no breakpoints, watches, or debug windows. Additionally, the Debug object is not supported. VBScript does not allow you to "step" through code, either; however, stepping can be simulated if you use the MsgBox function periodically in your code to report information. These MsgBox functions can then be commented out or removed from the final document.
VBScript does not support any of the file operations found in Visual Basic for Applications, such as reading and writing to a file. Once again, this is because of the risks involved in being able to access the client machine without restriction. Clearly, allowing direct file I/O from the browser places the client machine at the mercy of any rogue executable that gets downloaded.
VBScript provides support for forms. Forms are created with the <FORM> </FORM> tags. The fundamental difference between VBScript forms and Visual Basic for Applications forms is that VBScript forms are not visible as separate "windows" in the application. Instead, forms are a way to group controls together for the purpose of referencing their properties and methods in code or submitting data to a back-end process.
VBScript does not allow access to the Windows help file system. Unlike Visual Basic for Applications, VBScript also cannot produce context-sensitive help that displays topics for objects that currently have the focus. Help is once again limited to the text placed inside an HTML file.
Fortunately, the hypertext links available in HTML are a convenient way to provide help. For example, the language documentation for VBScript is available on this book's companion CD as an HTML file. If you run this HTML file, you will find that the hypertext links work in a way that is similar to the way any Windows-based help file system works.
VBScript does not support intrinsic constants, which are built-in, predefined constants. For example, to create a message box with Abort, Retry, and Ignore buttons, you would normally specify the second argument in the MsgBox statement as vbAbortRetryIgnore. However, since VBScript does not recognize intrinsic constants, you must specify the numeric values directly in code.
VBScript does not support menu structures similar to those found in Windows-based applications. Windows-style menus are typically found only in the browser itself and are used to navigate the browser. You can create menu structures, however, either by utilizing HTML directly or through an ActiveX control, but such menus do not have the same appearance or interaction as true drop-down menus.
In HTML, you can implement a menu structure using the anchor tag, <A></A>. The anchor tag denotes a link to another URL. The anchor tag has an HREF attribute that directs the browser to a new URL when the text in the anchor is clicked. The following code sets up a link to the Microsoft web site when the word JUMP is clicked:
<A HREF="http://www.microsoft.com">JUMP</A>
Using the anchor tag, you can set up a group of jumps that can form a simulated menu structure. HTML even supports a <MENU></MENU> tag that displays the items as a logical grouping. The following code is an example of a menu offering links to various sites:
<MENU>
<LI><A HREF="http://www.microsoft.com">
Microsoft</A></LI>
<LI><A HREF="http://www.vb-bootcamp.com">
New Technology Solutions, Inc.</A></LI>
<LI><A HREF="http://www.anysite.com">
Any Site</A></LI>
</MENU>
This text appears in the browser as a grouping of links that is displayed statically on the page. The menu does not drop down and cannot be hidden. It is simply a section of text links.
ActiveX controls, however, can offer some solutions to the menu problem posed above. Utilizing an ActiveX control specifically designed to implement menus, you can create a pop-up menu that displays a series of menu choices. The pop-up menu control can be displayed on demand and is automatically hidden after a choice is made. Listing 2-3 shows a complete example using the ActiveX Popup Menu control to implement a menu. Figure 2-3, on page 35, shows a sample of the output. Note that, in order for the program in Listing 2-3 to work properly, the mnuPopUp control must reside and be registered on your computer. ActiveX controls are covered in detail in Chapter 4.
Listing 2-3.
Using the ActiveX Popup Menu control to add a menu to a web page.
<HTML>
<HEAD>
<TITLE>Pop-up Menu</TITLE>
<!--
This demo uses an ActiveX
Popup Menu control to
implement a pop-up menu in
a web page
-->
<SCRIPT LANGUAGE="VBScript">
Sub cmdAdd_OnClick
'Author: New Technology Solutions, Inc.
'Purpose: Add a new item to the menu
'6/2/96 Original
Dim MyForm
Set MyForm=Document.frmPopUp
MyForm.mnuPopUp.AddItem MyForm.txtMenu.Value
MsgBox "New Item Added!"
End Sub
Sub cmdClear_OnClick
'Author: New Technology Solutions, Inc.
'Purpose: Clear all menu items
'6/2/96 Original
Dim MyForm
Set MyForm=Document.frmPopUp
MyForm.mnuPopUp.Clear
MsgBox "Menu Cleared!"
End Sub
Sub cmdPop_OnClick
'Author: New Technology Solutions, Inc.
'Purpose: Cause the menu to appear
'6/2/96 Original
Dim MyForm
Set MyForm=Document.frmPopUp
MyForm.mnuPopUp.PopUp
End Sub
Sub mnuPopUp_Click(intIndex)
'Author: New Technology Solutions, Inc.
'Purpose: Receive the click from the menu
'6/2/96 Original
Dim MyForm
Set MyForm=Document.frmPopUp
MsgBox "You clicked Item #" & intIndex
End Sub
</SCRIPT>
</HEAD>
<BODY BGCOLOR="WHITE">
<CENTER>
<FORM NAME="frmPopUp">
<INPUT TYPE="TEXT" VALUE="MenuItem" NAME="txtMenu">
<P>
<INPUT TYPE="BUTTON" VALUE="AddItem" NAME="cmdAdd">
<P>
<INPUT TYPE="BUTTON" VALUE="Clear" NAME="cmdClear">
<P>
<INPUT TYPE="BUTTON" VALUE="Pop-up Menu" NAME="cmdPop">
<P>
<OBJECT
CLASSID="clsid:7823A620-9DD9-11CF-A662-00AA00C066D2"
ID="mnuPopUp"
WIDTH=80
HEIGHT=30
>
</OBJECT>
</FORM>
</CENTER>
</BODY>
</HTML>
VBScript supports code modules through the <SCRIPT></SCRIPT> tag. Each script section forms an independent code module that can have its own variables, functions, and subroutines. These modules are similar to the standard BAS module found in Visual Basic.
There is no concept of a Multiple Document Interface (MDI) application in VBScript. MDI applications are programs that display multiple forms inside a large parent window. The child windows are confined to the boundaries of the parent window. Since VBScript does not present forms as independent objects, you cannot create MDI applications in a web browser.
Figure 2-3.
Sample output of the ActiveX Popup Menu control.
As in Visual Basic for Applications, undeclared variables in VBScript are automatically dimensioned as variants. No specific requirement exists to declare variables before you use them. However, failing to declare variables can still lead to bugs in your code. Although VBScript supports only the variant data type, Option Explicit can still be valuable because it will automatically detect any typographical errors in variable names. (The Option Explicit statement requires the explicit declaration of all variables.) Therefore, we recommend the use of Option Explicit inside every set of <SCRIPT></SCRIPT> tags. When using Option Explicit, you must remember to make it the first line of code in any script section and that the scope of Option Explicit is only inside the current <SCRIPT></SCRIPT> tags.
Although Visual Basic for Applications supports the Screen, Printer, App, Debug, Err, and Clipboard objects, VBScript supports only the Err object. Therefore, VBScript does not allow you to access such useful objects as the mouse pointer or the clipboard. You can, however, use the Err object to provide runtime error handling for your applications.
Although Visual Basic for Applications supports a myriad of different data types, VBScript supports only the variant data type. The variant is a data type that can contain any kind of variable data (integers, strings, and objects, for example). Variables are discussed in detail later in this chapter.
VBScript does not allow web pages to make calls to the Windows application programming interface (API). Allowing such calls could jeopardize the security of the client. For example, viruses could easily use API calls to damage the client machine.