Developing Interactive Web Pages Using VB Scripts
Developing Interactive Web Pages Using VB Scripts
What is VBScript?
When a VBScript is inserted into a HTML document, the Internet browser will read the
HTML and interpret the VBScript. The VBScript can be executed immediately, or at a later
event.
VBScript is designed as an extension to HTML. The web browser receives scripts along with
the rest of the web document. HTML is extended to include a tag that is used to incorporate
scripts into HTML. 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.
Let's start with an easy program and then develop more advanced programs.The examples
below show how these codes are used and some of the basic VBScript commands.
This sample program demonstrates simple displaying of messages in response to the click of
a button.
252
As you can see, clicking the button produces the message "Hello, world!", with the caption of
the message box reading "Muhammed Ucar's VBScript Tutorial". The code used to produce
this began with a simple HTML button:
A Visual Basic Script subroutine was then implemented in the <SCRIPT> tag of the Web
page:
<SCRIPT LANGUAGE="VBScript">
Sub cmdHello_OnClick()
MsgBox "Hello, world!", vbOKOnly, "Ben's VBScript Tutorial"
End Sub
</SCRIPT>
Example 2 shows a more useful example that changes the page's text based on user response.
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.
253
Example 3:Getting Simple Input From the User
This program asks for your name and then displays it in a text box. You can have the
program say "Hi" to you by clicking the second button.
This example also uses button controls, but adds a text box as well:
In addition, the MsgBox statement is used once again to display a message - but this time it's
a message that can change with the text box, instead of being set, unchanging information.
First, we set the value of the textbox by getting input:
<SCRIPT LANGUAGE="VBScript">
Sub cmdGetName_OnClick()
Dim MyName
MyName = InputBox("Hello! What's your name?","Getting Input")
txtName.Value = MyName
MsgBox "Hi, " & txtName.Value & "! How are you?", vbOKOnly, "Ben's VBScript
Tutorial"
End Sub
Sub cmdShowName_OnClick()
MsgBox "Hi, " & txtName.Value & "! How are you?", vbOKOnly, "Ben's VBScript
Tutorial"
End Sub
</SCRIPT>
Dim means we're going to dimension a variable. In VBScript a variable is an object that can
hold a value. This can be a string value ("Hello world!") or a numeric value (3454) or other
things. In this case we're assigning MyName a string value. The InputBox statement gets
input from the user by displaying a window which has a textbox in it. As with MsgBox, you
can specify a prompt and a title in the parameters, but a function requires that you put
parentheses around its arguments (parameters).
254
Then we assign the MyName variable to txtName.Value. First we specify the name of the
object to assign it to (txtName), and then we put a period in between that and the property we
wish to change, in this case, the Value.
Now we demonstrate that strings can be put together to form a larger string. The first
parameter in the MsgBox statement reads "Hi, " & txtName.Value & "! How are
you?" The first part of the string is "Hi, ", but then the name that the user typed in is
used: & txtName.Value & "! How are you?"
The next Sub (for the cmdShowName button) repeats the last command in the other button.
This program demonstrates manipulating the color properties of the document. You can use
VBScript to change the background color, the text color, and link color of the Web page.
The source code for this is not really very difficult, it just shows a few more of the things
possible with VBScript.
As usual, the buttons are made with standard HTML tags, and the functionality is built into
the VBScript block.
<SCRIPT LANGUAGE="VBScript">
Function DRGB(Red, Green, Blue)
Dim r, g, b
If Len(Hex(Red)) < 2 Then
r = "0" & Hex(Red)
Else
r = Hex(Red)
End If
If Len(Hex(Green)) < 2 Then
g = "0" & Hex(Green)
Else
g = Hex(Green)
End If
255
If Len(Hex(Blue)) < 2 Then
b = "0" & Hex(Blue)
Else
b = Hex(Blue)
End If
DRGB = r & g & b
End Function
Sub cmdDocWhite_OnClick()
Document.BGColor = DRGB(255,255,255)
End Sub
Sub cmdDocBlack_OnClick()
Document.BGColor = DRGB(0,0,0)
End Sub
Sub cmdTBlue_OnClick()
Document.FGColor = DRGB(0,0,255)
End Sub
Sub cmdTRed_OnClick()
Document.FGColor = DRGB(255,0,0)
End Sub
Sub cmdTBlack_OnClick()
Document.FGColor = DRGB(0,0,0)
End Sub
</SCRIPT>
VBScript hex values differ from the way Internet Explorer interprets the colors, which is why
the DRGB function exists. By using the DRGB function instead of VBScript's RGB,
document colors will show up the way you intended them to be.
This example shows how to add an ActiveX™ control (OCX) to your web page. It asks the
user to enter number in the textbox and when the user clicks button, the number is shown in
LCD display (ActiveX component) which is embedded to the web page.
256
the <OBJECT> tag is used to place ActiveX controls in HTML pages.
ID= A name for the object. The name should be unique among all other objects on the
page.CLASSID=A class ID number. Every ActiveX control has a unique class ID that was
encoded into it during development. The browser uses this number to determine which
control to load.This number must be exactly correct or the control will not load. Thus, it is
preferable to cut and paste these numbers into your object tag or to use a tool such as the
ActiveX Control Pad to insert them. For more information, see "Using the ActiveX Control
Pad." CODEBASE= URL referring to where the ActiveX control can be acquired. When the
page is loaded into the user's system, the browser checks the registry to determine if the
system has the ActiveX control referenced by the CLASSID. If it does, then the local copy is
used. Otherwise, the browser attempts to download the control from the URL designated by
the CODEBASE tag.PARAM NAME="x" VALUE="y" Optional parameters to set
properties of the ActiveX control when it is loaded. Each is expressed as a property NAME
257
and a VALUE to set it to. Once the control is loaded, it is given these property settings by the
browser. Which settings are supported depends on the ActiveX control.
258