Beruflich Dokumente
Kultur Dokumente
VBScript, Microsoft's Visual Basic Scripting Edition, is a scaled down version of Visual
Basic. While it doesn't offer the functionality of Visual Basic, it does provide a
powerful, easy to learn tool that can be used to add interaction to your web pages.
If you are already experienced in either Visual Basic or Visual Basic for Applications,
you will find working with VBScript easy and should be immediately productive.
Don't be concerned if you haven't worked in another version of Visual Basic.
VBScript is easy to learn, even for the novice developer.
You add scripts into your web pages within a pair of <SCRIPT> tags. The <SCRIPT>
tag signifies the start of the script section, while </SCRIPT> marks the end. An
example of this is shown below:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="VBScript">
</SCRIPT>
The beginning <SCRIPT> tag includes a LANGUAGE argument that indicates the
scripting language that will be used. The LANGUAGE argument is required because
there is more than one scripting language. Without the LANGUAGE argument, a web
browser would not know if the text between the tags was JavaScript, VBScript or
another scripting language.
While technically you can place scripts throughout an HTML document using pairs of
<SCRIPT> tags, typically scripts are often found at either the top or bottom of a
Web document. This provides for easy reference and maintenance.
Not all browsers support scripting languages. Some only support JavaScript. Only
Microsoft's Internet Explorer supports VBScript. You might be wondering what
happens to your scripts when non-supporting browsers encounter them. Usually
browsers will do what they do most frequently with text, they will display your
scripts as part of the web page. Obviously, this isn't the result you had hoped for.
One simple way to address this problem is to encase your scripts in comment tags
(<!-- and -->). Below is our example script as it appears with the addition of the
comment tags:
<HTML>
<HEAD>
<SCRIPT LANGUAGE="VBScript">
<!--
-->
</SCRIPT>
</HEAD>
</HTML>
Now, when a browser that does not support VBScript processes this page, it will
view your script as a comment and simply ignore it.
A variable is a named location in computer memory that you can use for storage of
data during the execution of your scripts. You can use variables to:
• Store input from the user gathered via your web page
An Introduction to Variables
Sub cmdVariables_OnClick
Dim Name
End Sub
The first line of this example defines a sub procedure associated with the click event
of a command button named cmdVariables.
On the second line we declare a variable named Name. We are going to use this
variable to store the name of the user when it is entered. The third line uses the
InputBox function to first prompt for, and then return, the user's name. You will see
more of the InputBox function later in this tutorial. The name it returns is stored in
the Name variable.
The fourth line uses the MsgBox function to display the user's name. Finally, the sub
procedure completes on line five.
Exactly how, and where, variables are stored is not important. What you use them
for, and how you use them is important. That is what we will be looking at next.
Declaring Variables
There are two methods for declaring variables in VBScript, explicitly and implicitly.
You usually declare variables explicitly with the Dim statement:
Dim Name
This statement declares the variable Name. You can also declare multiple variables
on one line as shown below, although it is preferable to declare each variable
separately:
Variables can be declared implicitly by simply using the variable name within your
script. This practice is not recommended. It leads to code that is prone to errors and
more difficult to debug.
You can force VBScript to require all variables to be explicitly declared by including
the statement Option Explicit at the start of every script. Any variable that is not
explicitly declared will then generate an error.
• They must be unique within the same scope. There is more on scopes later in
this lesson
VBScript has a single data type called a variant. Variants have the ability to store
different types of data. The types of data that a variant can store are referred to as
subtypes. The table below describes the subtypes supported by VBScript.
Subtype Description of Uses for Each Subtype
Object Objects
Assigning Values
Variable_name = value
HoursWorked = 50
Overtime = True
Scope of Variables
The scope of a variable dictates where it can be used in your script. A variable's
scope is determined by where it is declared. If it is declared within a procedure, it is
referred to as a procedure-level variable and can only be used within that procedure.
If it is declared outside of any procedure, it is a script-level variable and can be used
throughout the script.
<SCRIPT>
Dim counter
Sub cmdButton_onClick
Dim temp
End Sub
</SCRIPT>
The variable counter is a script-level variable and can be utilized throughout the
script. The variable temp exists only within the cmdButton_onClick sub-procedure.
Constants
VBScript does not provide support for constants, such as you find in other
programming languages. You can work around this by assigning values to variables
that you have defined as shown in the example below. Here, TAX_RATE is our
constant.
<SCRIPT>
Dim TAX_RATE
TAX_RATE = .06
Function CalculateTaxes
End Function
</SCRIPT>
Arrays
The VBScript language provides support for arrays. You declare an array using the
Dim statement, just as you did with variables:
Dim States(50)
The statement above creates an array with 51 elements. Why 51? Because VBScript
arrays are zero-based, meaning that the first array element is indexed 0 and the last
is the number specified when declaring the array.
You assign values to the elements of an array just as you would a variable, but with
an additional reference (the index) to the element in which it will be stored:
States(5) = "California"
Dim StateInfo(50,1)
To store values into this array you would then reference both dimensions.
StateInfo(18,0) = "Michigan"
StateInfo(18,1) = "Lansing"
VBScript also provides support for arrays whose size may need to change as the
script is executing. These arrays are referred to as dynamic arrays. A dynamic array
is declared without specifying the number of elements it will contain:
Dim Customers()
The ReDim statement is then used to change the size of the array from within the
script:
ReDim Customers(100)
There is no limit to the number of times an array can be re-dimensioned during the
execution of a script. To preserve the contents of an array when you are re-
dimensioning, use the Preserve keyword:
Objects, both in the form of Java applets and ActiveX controls, enhance the
functionality that is provided with HTML. By using VBScript you can extend the
capabilities of these controls, integrating and manipulating them from within your
scripts. In this lesson we will look at how you can utilize the power of objects with
VBScript.
Since this is a VBScript tutorial, rather than an HTML tutorial, we will offer only a
limited discussion of how to add an object to a web page. Objects, whether they're
Java applets or ActiveX controls are added to a page with the <OBJECT> tag. The
properties, or characteristics, of the object are configured using the <PARAM> tag.
Typically you will see an object implemented using a single <OBJECT> tag along
with several <PARAM> tags. The following HTML code demonstrates how an ActiveX
control might appear when added to a page:
CLASSID="CLSID:978C9E23-D4B0-11CE-BF2D-00AA003F40D0">
Once you have added a control to your web page, it can be configured, manipulated
and responded to through its properties, methods and events. Properties are the
characteristics of an object. They include items like a caption, the foreground color
and the font size. Methods cause an object to perform a task. Events are actions
that are recognized by an object. For instance, a command button recognizes an
onclick event.
Note
The Script Wizard found in the Microsoft ActiveX Control Pad can be used to identify
events provided by a control, and to generate script to respond to these events.
For the most part, you will be focusing on properties and events. An example of
setting properties for a label control is shown in the following example.
<SCRIPT LANGUAGE="VBScript">
Sub cmdCalculatePay_onClick
Dim HoursWorked
Dim PayRate
Dim TotalPay
lblTotalPay.caption = TotalPay
End Sub
</SCRIPT>
The caption property of the label control, lblTotalPay, is set equal to the results of
our calculation with the script line:
document.frmPayrate.lblTotalPay.caption = TotalPay
Object properties are referenced within your scripts using the same format shown in
Exercise 2.
VBScript allows you to control how your scripts process data through the use of
conditional and looping statements. By using conditional statements you can develop
scripts that evaluate data and use criteria to determine what tasks to perform.
Looping statements allow you to repetitively execute lines of a script. Each offers
benefits to the script developer in the process of creating more complex and
functional web pages.
Conditional Statements
If..Then..Else
Select..Case
If..Then..Else
The simplest version of an If statement is one that contains only a condition and a
single statement:
If AmountPurchased > 10000 Then
DiscountAmount = AmountPurchased * .10
Next we will look at a more complicated version of the If statement. In this version
we will perform a series of statements when the condition is true:
End If
In this form of the If statement, one or more statements can be executed when the
condition is true, by placing them between the If statement on top and the End If
statement on the bottom.
The next form of the If statement uses the If..Then..Else format. This version of the
If statement differs from the two previous versions in that it will perform one set of
statements if the condition is true and another set when the condition is false:
Else
End If
In this example when the condition is true, that is the customer's order is over
$10,000, they receive a 10% discount. When the order is under $10,000, they are
charged a 3% handling fee.
The final version of the If statement that we will look at is the If..Then..Else If. In
this form the If statement checks each of the conditions until it either finds one that
is true or an Else statement:
If AmountPurchased > 10000 Then
Else
End If
In this example the customer receives a 10%discount for orders over $10000, a 5%
discount for orders over $5000 and a handling fee of 3% for orders under $5000.
As you see, VBScript offers you plenty of options when it comes to If statements.
Select Case
Case value
Case value
...
Case Else
End Select
For example, the following Select statement assigns different shipping fees based
upon the State where the order is being sent:
ShippingFee= .04
Case "Florida"
ShippingFee = .03
Case Else
ShippingFee = .02
End Select
The Select Case statement checks each of the Case statements until it finds one that
will result in the condition being true. If none are found to be true, it executes the
statements within the Case Else.
Note
Even though it is not required, always include a Case Else when working with Select
Case statements to process conditions that you may not have considered possible.
For these conditions you can display something as simple as a message dialog to
inform you that a branch was executed that you hadn't planned for.
As the popularity of web page forms increase, so does the need to be able to
validate data before the client browser submits it to the web server. As a scripting
language, VBScript is well suited for this task. Once the form has been validated, the
same script can be used to forward the data on to the server. In this lesson we will
look at both the process of validating and submitting forms.
The process of validating forms involves checking the form to see if:
Meticulous data validation scripts can be tedious to code but are well worth their
return in verifying the quality of the data.
The validation example that we will be examining does not contain anything new in
the way of VBScript. We are simply using the elements that we have learned in the
previous lessons in a new way. Before reading any further you may find if beneficial
to ponder how you would validate an HTML form using the VBScript techniques that
you have learned.
Okay, are you through pondering? Let's look at an example to give you an idea of
what is possible when it comes to validating forms.
Checking Form Input
This example is pretty simple. It has a single field in which the user can enter their
age and a single command button that is used to submit their age to the server. A
copy of this example can be found in exam_5a.htm.
<HTML>
<HEAD>
<SCRIPT LANGUAGE="VBScript">
Option Explicit
Sub cmdSubmit_OnClick
If (Len(document.frmExample5a.txtAge.value) = 0) Then
Exit Sub
End If
If (Not(IsNumeric(document.frmExample5a.txtAge.value))) Then
Exit Sub
End If
If (document.frmExample5a.txtAge.value < 0) Or _
Exit Sub
End If
document.frmExample5a.submit
End Sub
-->
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="frmExample5a">
<TABLE>
<TR>
<TR>
<TD></TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
How It Works
The heart of this validation script is found in the click event procedure for the
cmdSubmit command button. We start by checking if the user entered anything at
all into the field using VBScript's Len function. This function returns the length of a
string. If the length is 0, the data is invalid. We inform the user and exit the submit
procedure via the Exit Sub statement:
Exit Sub
End If
Next we check to see if what the user entered is a numeric value. The VBScript
function IsNumeric returns a true value when it is a number. If not, we tell the user
and exit:
If (Not(IsNumeric(document.frmExample5a.txtAge.value))) Then
Exit Sub
End If
Our final check involves verifying that the age they entered seems reasonable for
our environment. I have determined that no age less than 0 or greater than 100 is
acceptable. Using an If..Then statement we can check the value of the input field
against this criteria:
' Check to see if the age entered is valid.
If (document.frmExample5a.txtAge.value < 0) Or _
(document.frmExample5a.txtAge.value > 100) Then
MsgBox "The age you entered is invalid."
Exit Sub
End If
That's it. While this example is by no means the most detailed validation script you
will encounter it provides you with a basis of what is possible with VBScript.
The code that we would have to add to our previous example to submit the form is
shown below:
The MsgBox statement lets the user know that their data has been processed. The
form is then submitted by invoking the Submit method of the form object. As we
saw in lesson 3 on objects, methods cause an object to perform a task. Here we are
using the submit method of our form to cause the form to submit its data, just as if
we had used a submit control.
Applet
width=200></applet>*/
import java.applet.*;
import java.net.*;
import java.awt.*;
String msg;
setBackground(Color.red);
g.drawString(msg,10,20);
<HTML><HEAD></HEAD><BODY>
</BODY>
</HTML>