VBScript Tutorial


Motivation

VBScript is one of a number of technologies (including Active Server Pages (ASP), JavaScript) available to incorporate interactive elements on a web page. Standard HTML code is static - whatever elements the author incorporated into a page will not chnge unless the HTML code is changed. However, incorporating a script into a web page allows certain elements to respond to user input. For example, VBscript can be used on a form to verify that information being submitted conforms to the desired format. VBScript is one implementation of Dynamic HTML (dHTML) - a method of changing the appearance of a web page "on the fly" and without intervention from the author.


Objectives & Materials Required

The intent of this page is to provide some very basic example of how VBscript can be incorporated into HTML to "liven-up" a web page. It assumes the user has a basic understanding of the Web and HTML authoring as well as working knowledge of Visual Basic programming. See the references in the conclusions section below for tutorials on these topics.

VBScript was first introduced in Microsoft Internet Explorer 3.0 and is automatically installed by Windows 95a or later, Windows NT 4 or later, Internet Explorer 3 or later, Internet Information Server 3 or later, Outlook 98, Visual Studio 98, and Office 2000. To check if it installed on your machine, look for the SCRRUN.DLL file in your system directory (\Windows\System or \WinNT\System32 folder). If needed, the latest version of this file can be downloaded from Microsoft's Web site.


Introduction

What is VBScript?

VBScript is a scripting language developed by Microsoft to extend the functionality of standard HTML. The language is a subset of the Visual Basic Programming Language and has a very similar command syntax. The technology was developed in response to competing technologies (such as JavaScript) and as a way of expanding the usefulness and functionality of standard HTML.

How is it Used?

VBscript can be incorporated into a web page by adding a few commands to the HTML code. First, the <SCRIPT Language="VBScript"> tag tells the browser that the page to be displayed contains a script and it is of the type VBScript. Because not all browsers are able to processVBScript, the rest of the code should be enclosed in comment tags <!-- --> so that these browsers will ignore the code and not clutter the page. The examples below show how these codes are used and some of the basic VBScript commands.


Some Examples...

Note: These examples require a VBScript capable browser. We recommend Microsoft's Internet Explorer for Windows.

Example 1: Basic Hello World

The first example is - of course - a program to display the "Hello World!" message on the web-page.

<HTML>
<SCRIPT Language="VBScript">
<!--
Option Explicit
document.write("<h1>Hello World!</h1>")
-->
</SCRIPT>
 
<HEAD>
<TITLE>Example 1: Hello World</TITLE>
</HEAD>
<BODY>
</BODY>
</HTML>

Click Hello World, Example 1 to see what this code will do.

The "HelloWorld!" message could have just as easily been displayed using standard HTML code. Example 2 shows a more useful example that changes the page's text based on user response.


Example 2: Hello World

Here the script displays a message box, then writes the user's response to the page.

<HTML>
<HEAD>
<TITLE>Example 1B: Hello World</TITLE>
</HEAD>
 
<BODY>
<SCRIPT Language="VBScript">
<!--
dim myVal, btnPressed
myVal = msgBox ("Hello World!", vbokcancel, "Hello")
select case myVal
case 1 btnPressed="OK"
case 2 btnPressed="Cancel"
case else btnPressed="Error!"
end select
 
document.write ("<h1>You selected the " & btnPressed & " button.</h1>")
-->
</SCRIPT>
 
</BODY>
</HTML>

Click Hello World, Example 2 to see what this code will do.

This version of Hello World demonstrates how VBScript can be used to get some input from the user and then modify the appearance of the displayed HTML code.

 


Example 3: Hello World with ActiveX Control

This final example shows how VBScript can be used to interface with ActiveX controls embedded on a web page. This example prompt the user with a Hello World dialog box containing "OK" and "Cancel" buttons. Once the user the selects a button to close the Hello World message, the response is parsed and passed to the ActiveX control (rtbDisplay) to be displayed on screen.

<HTML>
<HEAD>
<TITLE>New Page</TITLE>
</HEAD>
<BODY>
<CENTER>
<OBJECT ID="rtbDisplay" WIDTH=200 HEIGHT=40
CLASSID="CLSID:3B7C8860-D78F-101B-B9B5-04021C009402">
<PARAM NAME="_ExtentX" VALUE="5292">
<PARAM NAME="_ExtentY" VALUE="1058">
<PARAM NAME="_Version" VALUE="393217">
<PARAM NAME="BackColor" VALUE="16777215">
<PARAM NAME="Enabled" VALUE="1">
</OBJECT>
</CENTER>
 
<SCRIPT Language="VBScript">
<!--
dim myVal, btnPressed
myVal = msgBox ("Hello World!", vbokcancel, "Hello")
select case myVal
case 1 btnPressed="OK"
case 2 btnPressed="Cancel"
case else btnPressed="Error!"
end select
rtbDisplay.TextRTF = "You pressed the " & btnPressed & "button."
-->
</SCRIPT>
 
</BODY>
</HTML>

Click Hello World, Example 3 to see what this code will do.


Hardware Applications

Though VBScript can't directly control hardware attached to the computer it is still possible to use the scripting technology to control peripherals. As we demonstrated in Example 3, ActiveX controls can be embedded on a web page and controlled by VBScript. In a previous tutorial we created a client/server system that allowed a keypad connected to a client computer to control peripherals attached to a server using an 8255 PC Interface Card. This system could be extended so that the client would run as a VBScript with an embedded ActiveX Winsock contol to communicate to the server over the internet.


Conclusion

What else can we do with VBScript? As mentioned in the introduction, this is merely a bnrief tutorial on VBScript technology and its uses on webpages. For additional information, check out these websites and additional resources.

VBScript

Web Resources

Books & Publications

WWW & HTML

Web Resources

Books & Publications


Return to Final Exam Page

For questions or comments regarding this web page contact me at: gsagolla@dca.net.

Last Updated: 03-Jun-2001