Windows NT Internet and Intranet Development

To learn more about author Sanjaya Hettihewa, please visit the author's homepage.

Previous chapterNext chapterContents


- 13 -

Introduction to VBScript


VBScript, developed by Microsoft, is a powerful, lightweight, easy-to-use, freely available, cross-platform, and cross-language scripting language for the Internet. VBScript is designed to leverage the skills of millions of Visual Basic programmers to the Internet. Before proceeding, let's quickly examine how and why VBScript is equipped with each of the features enumerated here.


URL:Visit the following Web page to learn about VBA features not supported by VBScript:

http://www.microsoft.com/vbscript/us/vbslang/vsgrpNonFeatures.htm

Providing scripting, automation, and customization capabilities for Web browsers is a major feature of VBScript. If you are already familiar with Visual Basic, you will be able to leverage your skills to the Internet using VBScript very shortly. Even if you are not familiar with another programming language, you will be able to create active Web pages using VBScript after reading this section. However, familiarity with a programming language will make it easier for you to grasp various concepts, such as type casting and Boolean arithmetic, presented in later chapters of this section.


Note:Several VBScript applications are included in the CD-ROM; experiment with them to become more familiar with VBScript.



URL:Visit the Microsoft VBScript Web site for the latest information about VBScript.

http://www.microsoft.com/VBScript

How VBScript Works

VBScript programs are defined between the HTML tags <SCRIPT LANGUAGE=VBScript> and </SCRIPT>. Browsers that support VBScript read the VBScript application contained between these HTML tags and execute it after checking the code for syntax errors. Figure 13.1 shows how VBScript works.

Figure 13.1. How VBScript works.

If the application contains no syntax errors, it is executed on the Web browser. If any syntax errors are detected, they are flagged by the VBScript interpreter (as shown in Figure 13.2).

Figure 13.2. Syntax errors in VBScript programs are flagged by the VBScript interpreter.

To hide VBScript code from "technologically challenged" Web browsers, VBScript code can be enclosed in two HTML comment tags, as shown in the following code. This prevents technologically challenged Web browsers from attempting to display the VBScript application as though it is part of the HTML code of the Web page.

<SCRIPT LANGUAGE=VBScript>
<!-- To hide VBScript code from technologically challenged
     Web browsers
... VBScript code ...
!-->
</SCRIPT>

Hello World!

Writing the classic Hello World! application with VBScript is very easy. This example shows how to create a Web page similar to the one shown in Figure 13.3. This Web page has three buttons; the first displays a message box with a greeting, the second displays the current time, and the third displays today's date.

If you want to experiment with the VBScript application shown in Figure 13.3, it can be found in the CD-ROM kit that accompanies this resource library in the directory \Chap-13\Hello.htm. Various key elements of the Hello World! VBScript application are outlined next.

The Hello World! Dialog Box

The Hello World! dialog box (shown in Figure 13.4) is displayed each time a user clicks the Please click here for message box button in Figure 13.3. If you look at the HTML page of the VBScript application (see Listing 13.4), you will see that the command button associated with the Hello World! dialog box is named BtnHello (NAME="BtnHello"). As shown in Listing 13.1, the OnClick event is associated with the BtnHello subroutine. Each time a user clicks the Please click here for message box button in Figure 13.3, the Web browser invokes the BtnHello_OnClick subroutine, and any VBScript code defined in that subroutine is executed.

Figure 13.3. The classic Hello World! application written with VBScript.

The BtnHello_OnClick subroutine is very simple. The first three lines of code create strings displayed in the dialog box shown in Figure 13.4. Note how the string concatenation operator (&) is used in line 4 to merge two strings and assign the resulting value to a variable. The result is displayed in the message box in Figure 13.4.


Note:Line numbers are not part of the VBScript code. Line numbers have been inserted to make it easier to refer to various lines of code.

Listing 13.1. BtnHello_OnClick subroutine.

1: Sub BtnHello_OnClick
2:  titleString = "Windows NT Internet and Intranet Development"
3:  helloString = "Hello world! Welcome to the fun filled "
4:  helloString = helloString & "world of VBScript programming!"
5:  MsgBox helloString, 0, titleString
6: End Sub

Figure 13.4. The Hello World! dialog box.

Time Dialog Box

The BtnTime_OnClick subroutine is very similar to the BtnHello_OnClick subroutine. The only difference is that rather than concatenating two strings, BtnTime OnClick concatenates a string with the result of a function. The time function returns the current time. As shown in Figure 13.5, line 3 of Listing 13.2 displays the current time in a dialog box.

Listing 13.2. BtnTime_OnClick subroutine.

1: Sub BtnTime_OnClick
2:  timeString = "So, you want to know the time? The time is " & time
3:  MsgBox  timeString , 0, "Time Dialog Box"
4: End Sub

Figure 13.5. The time dialog box.

Date Dialog Box

The Date dialog box displays the current date, as shown in Figure 13.6. As you can see in line 2 of Listing 13.3, the result of the Date function is concatenated with the string "Today's date is" to display the current date in a dialog box.

Listing 13.3. BtnDate_OnClick subroutine.

1: Sub BtnDate_OnClick
2:  dateString = "Today's date is " & Date
3:  MsgBox  dateString , 0, "Date Dialog Box"
4: End Sub

Figure 13.6. The Date dialog box.

For your reference, the full source code of the Hello World! application is given in Listing 13.4.

Listing 13.4. The Hello World! Web page.

<!--
© 1996 Sanjaya Hettihewa
http://www.NetInnovation.com/sanjaya
!-->
<HTML>
<HEAD>
<TITLE>VBScript Tutorial: Hello World!</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#0000FF"
      LINK="#B864FF" VLINK="#670000" ALINK="#FF0000">
<IMG SRC="vbscript.jpg"><P>
<B><FONT FACE="Comic Sans MS" SIZE=6 COLOR=RED>
VBScript Tutorial: <FONT></B>
<I><FONT FACE="Comic Sans MS" SIZE=5 COLOR=BLUE>
 "Hello World!" </I><P><FONT>
<form>
<INPUT TYPE=BUTTON VALUE="Please click here for message box"
       NAME="BtnHello">
<INPUT TYPE=BUTTON VALUE="What time is it?"
       NAME="BtnTime">
<INPUT TYPE=BUTTON VALUE="What date is it?"
       NAME="BtnDate">
</form>
<SCRIPT LANGUAGE="VBScript">
<!-- To hide VBScript code from technologically challenged browsers
Sub BtnHello_OnClick
 titleString = "Windows NT Internet and Intranet Development"
 helloString = "Hello world! Welcome to the fun filled "
 helloString = helloString & "world of VBScript programming!"
 MsgBox helloString, 0, titleString
End Sub
Sub BtnTime_OnClick
 timeString = "So, you want to know the time? The time is " & time
 MsgBox  timeString , 0, "Time Dialog Box"
End Sub
Sub BtnDate_OnClick
 dateString = "Today's date is " & DateValue(date)
 MsgBox  dateString , 0, "Date Dialog Box"
End Sub
!-->
</SCRIPT>
</BODY>
</HTML>

Summary

VBScript is Microsoft's scripting language for the Internet. It is designed to leverage the skills and investments of Visual Basic developers to the Internet. Compared to scripting languages like JavaScript, VBScript is easier to learn and use. Furthermore, because VBScript can be easily used to automate various ActiveX controls in a Web page, it can be used to develop sophisticated and intelligent Web applications.


Previous chapterNext chapterContents


© Copyright, Macmillan Computer Publishing. All rights reserved.