To learn more about author Sanjaya Hettihewa, please visit the author's homepage.
VBScript, a programming language for developing server-side and client-side Web applications, is a subset of Microsoft Visual Basic and is upwardly compatible with Visual Basic for Applications (VBA). VBA is used in Microsoft Office applications to build custom solutions using various Microsoft Office applications. If you are familiar with VBA or Visual Basic, you will feel right at home with VBScript.
VBScript is used by Web-page developers to add scripting, automation, and customization capabilities to Web pages and to develop richly interactive Web applications. If you're already familiar with Visual Basic or VBA, you'll be able to leverage your skills to the Internet using VBScript at the end of this section. Even if you're not familiar with Visual Basic or VBA, you'll be able to develop Web applications using VBScript at the end of this section because the syntax of VBScript is easy to understand. This chapter covers the fundamentals of VBScript Web-application development. If you're already familiar with the basics of VBScript, you might want to skim this chapter and proceed to Chapter 15, "Working with Data Structures."
You do not have to install VBScript per se. The VBScript engine is part of Internet Explorer and is installed on your system when you install Internet Explorer.
URL:Visit Microsoft's VBScript Web site athttp://www.microsoft.com/VBScript/for the most up-to-date information about VBScript.
See Figure 14.1 for a graphical representation of how client-side VBScript applications
are executed by Web browsers. VBScript applications are contained in HTML Web pages.
When a VBScript-compatible Web browser downloads a Web page containing VBScript code,
it executes that code whenever an object of the Web page triggers a VBScript subroutine
contained in the VBScript code.
Figure 14.1. How VBScript applications
are executed by Web browsers.
Before developing VBScript applications, it is important that you understand their structure. A typical VBScript application is composed of the following:
The HTML code may contain any number of valid HTML statements. Listing 14.1 contains valid HTML statements in every line of code except lines 23-42.
VBScript code delimiters separate VBScript code from the HTML code of a Web page. Lines 23-24 and 41-42 of Listing 14.1 are examples of VBScript code delimiters. Notice how the HTML comment tags (<!-- and -->) are used to enclose the VBScript source code. These tags prevent VBScript-challenged Web browsers from interpreting the VBScript source code as part of the text of the Web page and displaying it on the browser window.
A VBScript application is composed of one or more VBScript subroutines. VBScript subroutines are defined using the following syntax:
Sub <name of subroutine> ...VBScript statements... End Sub
VBScript subroutines are contained between the HTML tags <SCRIPT LANGUAGE="VBScript"> and </SCRIPT>. Certain events associated with objects of a Web page trigger VBScript subroutines to perform certain tasks. For example, when a user submits a form, a VBScript subroutine can validate the data entered by the user before the data is sent to the Web server for processing.
See lines 25-30, 32-35, and 37-40 in Listing 14.1 for examples of VBScript subroutines.
1: <HTML> 2: <HEAD> 3: <TITLE>VBScript Tutorial: Hello World!</TITLE> 4: </HEAD> 5: 6: <BODY BGCOLOR="#FFFFFF" TEXT="#0000FF" 7: LINK="#B864FF" VLINK="#670000" ALINK="#FF0000"> 8: 9: <B><FONT FACE="Comic Sans MS" SIZE=6 COLOR=RED> 10: VBScript Tutorial: <FONT></B> 11: <I><FONT FACE="Comic Sans MS" SIZE=5 COLOR=BLUE> 12: "Hello World!" </I><P><FONT> 13: 14: <FORM> 15: <INPUT TYPE=BUTTON VALUE="Please click here for message box" 16: NAME="BtnHello"> 17: <INPUT TYPE=BUTTON VALUE="What time is it?" 18: NAME="BtnTime"> 19: <INPUT TYPE=BUTTON VALUE="What date is it?" 20: NAME="BtnDate"> 21: </FORM> 22: 23: <SCRIPT LANGUAGE=VBS> 24: <!-- To hide VBScript code from technologically challenged browsers 25: Sub BtnHello_OnClick 26: titleString = "Windows NT Internet & Intranet Development" 27: helloString = "Hello world! Welcome to the fun filled " 28: helloString = helloString & "world of VBScript programming!" 29: MsgBox helloString, 0, titleString 30: End Sub 31: 32: Sub BtnTime_OnClick 33: timeString = "So, you want to know the time? The time is " & time 34: MsgBox timeString , 0, "Time Dialog Box" 35: End Sub 36: 37: Sub BtnDate_OnClick 38: dateString = "Today's date is " & DateValue(date) 39: MsgBox dateString , 0, "Date Dialog Box" 40: End Sub 41: --> 42: </SCRIPT> 43: 44: </BODY> 45: </HTML>
Prior to client-side scripting languages such as VBScript, Web pages were mostly static entities. Interactivity in a Web page required the execution of a CGI application on the server and the display of the results of the CGI application on the Web browser. Although this worked well for some applications, it tied up valuable network and system resources. VBScript allows Web-page developers to create multimedia-rich, interactive Web pages with great ease while conserving network bandwidth and Web-server system resources. The next sections discuss the role of VBScript in Web-page development.
ActiveX controls are powerful components that can be used to build sophisticated Web applications. By themselves, ActiveX controls are capable of performing limited tasks. For example, the Microsoft Forms 2.0 ComboBox ActiveX control is capable of displaying a list of items, and the Microsoft Forms 2.0 Image ActiveX control is capable of displaying a graphical image. VBScript is used to glue ActiveX controls together and develop more sophisticated Web pages. For example, a VBScript subroutine can allow a user to change the image displayed in a Microsoft Forms 2.0 Image ActiveX control when an image is selected using a Microsoft Forms 2.0 ComboBox ActiveX control.
VBScript is ideal for developing dynamic Web applications that immediately respond to user interactions. The Mr. Potato Head application discussed next is an example of a Web application that is better implemented with a client-side scripting language such as VBScript as opposed to a server-side CGI application.
The application in Figure 14.2 is the CGI version of Mr. Potato Head. After a user selects the physical attributes of Mr. Potato Head, that information is transmitted to the Web server. A CGI application creates a graphic of Mr. Potato Head according the physical attributes selected by the user and transmits the image to the Web browser as shown in Figure 14.3. The CGI implementation of Mr. Potato Head is network intensive because data must be transferred between the Web browser and the Web server each time a user makes a selection. The CGI implementation is also processor intensive for the Web server because it must process a CGI request and create a Mr. Potato Head each time the user makes a selection.
URL:Browse http://winnie.acsu.buffalo.edu/cgi-bin/potato-cgi to experiment with the CGI version of Mr. Potato Head.
Figure 14.2. Selecting physical properties
of Mr. Potato Head.
The ActiveX version of Mr. Potato Head (see Figure 14.4) addresses the limitations
of the CGI version. As you can see, the ActiveX version is more interactive and easier
to use because it allows the user to change physical attributes of Mr. Potato Head
on-the-fly without interacting with the Web server. Users can select physical attributes
and drag-and-drop them on Mr. Potato Head. The ActiveX version of Mr. Potato Head
is less processor intensive because it does not communicate with the Web server each
time the user makes a change; it is less network intensive because all the processing
is done locally.
Figure 14.3. Image of Mr. Potato Head
created by a CGI application.
URL:Browse the Web page at http://www.microsoft.com/ie/most/howto/layout/eggplant/begin.htm to experiment with the ActiveX version of Mr. Potato Head.
Figure 14.4. The ActiveX version of Mr. Potato Head.
Error checking is a very important aspect of Web-application development. Lack of error checking usually results in flaky applications that are frustrating to use. Listed next are examples of how VBScript is used to perform error checking and to validate data entered by users.
VBScript applications can modify Web-browser objects such as the background color of the current Web page. This feature is particularly useful when creating sophisticated Web applications. For example, a VBScript subroutine of a multi-frame Web application can change the contents of several frames when a user selects a URL or clicks a button. See Chapter 22, "Manipulating Web-Browser Objects and Events," for additional information about how VBScript applications can manipulate Web-browser objects and events.
When developing applications using VBScript (or any other programming language), you should create source code that is easy to maintain and read. Messy source code often leads to buggy applications that are hard to maintain and debug. The following tips will help you develop robust VBScript applications that are easy to maintain.
While indentation does not affect the way a VBScript application is executed, the lack of indentation can make an application extremely difficult to debug and maintain. You should indent control structures of applications as shown in Listing 14.2--particularly if it is a complex or large VBScript application. Also, do not be afraid to add blank lines between VBScript code segments to enhance readability and clarity.
Sub BtnEvaluate_OnClick IF (OperatorBox.Value = "?") THEN MsgBoxString = "A valid operator is required to carry out " MsgBoxString = MsgBoxString & "an evaluation." MsgBoxString = MsgBoxString & chr(10) MsgBoxString = MsgBoxString & "Valid operators are: +, -, *" MsgBox MsgBoxString , 48 , "Invalid operator!" ELSE IF (OperatorBox.Value = "+") THEN answer = CDbl(Operand1Box.Value) + CDbl(Operand2Box.Value) ELSEIF (OperatorBox.Value = "-") THEN answer = CDbl(Operand1Box.Value) - CDbl(Operand2Box.Value) ELSEIF (OperatorBox.Value = "*") THEN answer = CDbl(Operand1Box.Value) * CDbl(Operand2Box.Value) End IF MsgBox answer , 64 , "Results of calculation" Operand1Box.Value = answer Operand2Box.Value = 0 END IF End Sub Sub AddDigit ( digit ) REM Just in case there are any preceeding zeros or spaces Operand1Box.Value = CDbl (Operand1Box.Value) IF ( OperatorBox.Value = "?") THEN IF ( Len ( Operand1Box.Value ) < 14 ) THEN Operand1Box.Value = Operand1Box.Value & digit Operand1Box.Value = CDbl (Operand1Box.Value) ELSE TooManyDigits END IF ELSE IF ( Len ( Operand2Box.Value ) < 14 ) THEN Operand2Box.Value = Operand2Box.Value & digit Operand2Box.Value = CDbl (Operand2Box.Value) ELSE TooManyDigits END IF END IF End Sub
The code-continuation character is used to split relatively long VBScript statements. Generally, if a VBScript statement is over 80 characters long, you should use the code-continuation character to break the VBScript statement into two or more lines. This makes it easier to indent the VBScript application for easy reading. The code-continuation character is an underscore (_) placed at the end of the line to break a longer line as demonstrated in lines 2-4 of Listing 14.3. Notice how the code-continuation character makes the VBScript source code easier to read by preserving the indentation of the VBScript code.
1: Sub OperatorBox_OnChange 2: 3: IF (NOT((OperatorBox.Value = "+" ) OR _ 4: (OperatorBox.Value = "-" ) OR _ 5: (OperatorBox.Value = "*" ) OR _ 6: (OperatorBox.Value = "?" ))) THEN 7: MsgString = "Do not type invalid characters " 8: MsgString = MsgString & "into the operator text box! " 9: MsgString = MsgString & chr(10) 10: MsgString = MsgString & "The operator text box will now be reset." 11: MsgString = MsgString & chr(10) & chr(10) 12: MsgString = MsgString & "Valid input: +, -, *" 13: MsgBox MsgString , 48 , "Invalid input detected!" 14: OperatorBox.Value = "?" 15: END IF 16: 17: End Sub
Commenting your source code can save hours or even days. In the software-development industry, more time is spent maintaining existing applications than developing new applications. Commenting your source code makes it easier for you (or someone else) to understand your application and modify it without creating undue side effects. To insert a comment in a VBScript application, precede the comment with an apostrophe (`) or the keyword Rem like so:
` This is a comment
or
Rem This is a comment
VBScript is a powerful, light-weight scripting language for creating interactive, multimedia-rich, ActiveX-enhanced Web applications. Before client-side scripting languages such as VBScript, dynamic Web pages were created by server-side CGI applications. In some cases, the execution of server-side CGI applications is unnecessarily resource intensive. VBScript can be used in such cases to create dynamic content without any interaction with the Web server.
© Copyright, Macmillan Computer Publishing. All rights reserved.