Visual Basic Script, herein referred to as VBScript or VBS, is Microsoft's answer to Sun's JavaScript. Both are meant to be intertwined in HTML code; both are meant to expand on the client side functionality of the web; and neither of the two are fully released products. (VBScript is in beta with MS-Internet Explorer 3.0. JavaScript was first implemented in Netscape 2.0.)
http://www.microsoft.com/vbscript
The resource for information on Visual Basic Script is the Microsoft web site (http://www.microsoft.com/vbscript). Here you can find complete documentation, some sample snippets of VBScript code, and links to sites using VBScript.
In most languages, variables are an important part of the language, it's structure, and it's implementation. In VBScript, the variables are all of the same type, Variant.
The Variant type is an "all things to all people" generic data type. Assign it a string, and it acts as a string; assign it a number, and it acts like a number. The following lines are an example of declaring a Variant in VBScript:
<SCRIPT LANGUAGE="VBScript"> Option Explicit Dim vUserName, vUserNumber, vUserString </SCRIPT>
The SCRIPT LANGUAGE tag is a proposed addition to the HTML standard and is first implemented in the Microsoft Internet Explorer 3.0 It informs the browser that code, which must be interpreted, is coming and of what type it is. The usual as Variant, (like you would see in a regular Visual Basic program) is omitted because the Variant type is the only one available. Multiple variables per line are allowedthe procedure is to separate each by a comma. The Option Explicit keyword is implemented in Visual Basic Script, and if used, it should appear as the first statement after the <SCRIPT LANGUAGE = "VBScript"> tag.
The naming constraints in VBScript are similar to those of Visual Basic. Note that these rules apply to function names, constant names, and variable names. The name must begin with an alphabetic character. It cannot contain an embedded period, must be less than 255 characters, and it must be unique in the scope in which it is declared.
Two scopes are available in VBScript: the script level and procedure level. Variables at the script level are instantiated at the time the script is run, and exist until the script has completed running. Procedure level variables exist from the beginning of the procedure they are declared in, until the end of that procedure.
The Static keyword is supported when using persistent procedure level variables.
Arrays with multiple dimensions are also supported in VBScript:
Dim aSums(10) 'single dimension Dim aTable (10, 10) 'two dimension
As you can see, the declaration is just as in Visual Basic, except again there is no as <Variable Type>.
The ReDim and ReDim Preserve keywords are also supported in the creation and recreation of arrays. They function in the same way as their Visual Basic counterparts.
VbScript includes all the operators found in Visual Basic: arithmetic (+, -, and so on.), logical (And, Or, Not, etc.), and comparison (=, < >, and so on.). VBScript also obeys the same rules for operator precedence as Visual Basic.
Operator precedence is a set of rules that determine what order operators are evaluated in if more than one operator is present in an expression.
The If-Then-Else, Do-Loop, While-Wend, For-Next, and For Each-Next control constructs are supported, just as they are in standard Visual Basic syntax.
Like its Visual Basic cousin, VBScript supports the Sub and Function procedures. Within the HTML file, the subroutines and function bodies must be placed before the code that calls them. It's advisable to put the VBScript functions and subroutines in the beginning of the <HEAD> portion of the HTML document. Then, following the subroutines and procedures, place the remainder of the VBScript code.
Visual Basic Script supports a subset of the Visual Basic statements. The following list are the functions supported:
Call Statement
Dim Statement
Do-Loop Statement
Erase Statement
Exit Statement
For-Next Statement
For Each-Next Statement
Function Statement
If-Then-Else Statement
Let Statement
LSet Statement
Mid Statement
MsgBox Statement
On Error Statement
Private Statement
Public Statement
Randomize Statement
ReDim Statement
Rem Statement
RSet Statement
Set Statement
Static Statement
Sub Statement
While-Wend Statement
The following is a list of functions and objects that are supported by Visual Basic Script and operate similarly to the VB counterparts:
Abs Function
Asc Function
Atn Function
Chr Function
Cos Function
Date Function
DateSerial Function
DateValue Function
Day Function
Exp Function
Hex Function
Hour Function
InputBox Function
InStr Function
Int, Fix Functions
LBound Function
LCase Function
Left Function
Len Function
Log Function
LTrim, RTrim, and Trim Functions
Mid Function
Minute Function
Month Function
MsgBox Function
Now Function
Oct Function
Right Function
Rnd Function
Second Function
Sgn Function
Sin Function
Sqr Function
Str Function
StrComp Function
String Function
Tan Function
Time Function
TimeSerial Function
TimeValue Function
UBound Function
UCase Function
Val Function
VarType Function
Weekday Function
Year Function
The following sections provide information regarding new functions or functions which behave differently than their VB counterparts.
The Array function returns a variant that contains an array. The following is an example:
Dim aNums as Variant aNums = Array(1, 2, 3, 4, 5)
This would create an array with five values, 1 through 5. If no arguments are passed to the array function, the array created is of zero length.
Accessing the elements of an array is done the same way in VBScript as in Visual Basic. For instance,
X = aNums(3)
would assign the value of the third position in the array aNums to the variable X.
The data conversion functions listed in Table B.1 take one Variant variable as an argument and return that value as the Variant subtype of that function.
For example, the CBool Function would take a normal Variant as an argument and return a value into a Variant with the subtype Boolean.
Function |
Description |
CByte
|
Variant to subtype byte
|
CDate
|
Variant to subtype date
|
CDbl
|
Variant to subtype double
|
CInt
|
Variant to subtype integer
|
CLng
|
Variant to subtype long
|
CSng
|
Variant to subtype single
|
CStr
|
Variant to subtype string
|
CVErr
|
Variant to subtype error |
The values passed to these conversion functions must be in the valid range for the type being converted to. For example, the argument passed to the CVErr function must be a valid error number.
The Is functions determine whether the variable is of that data type. The IsArray function returns True if the Variant type variable passed to the function is of subtype array. The other Is functions (IsDate, IsEmpty, IsError, IsNull, IsNumeric, and IsObject) behave in the same way.
At this point, VBScript supports only the Err object. This contains any information about runtime errors during the processing of the script.
The Description property can return or be set to a string that is displayed to the user when a runtime error occurs.
The HelpContext property returns or sets the help context ID used to find a topic in a help file.
HelpFile specifies or can be set to any path that contains the help file.
This corresponds to the error number of the Err object. It can be read or set at runtime.
The Source property Returns or sets the object or code that caused the error.
This clears all properties of the Err object.
To generate a runtime error, use the Raise method on the Err object. The arguments to the Raise method are number, source, description, helpfile, helpcontext.
These correspond to the properties of the Err object and enable the programmer to set these properties at the time the error is raised.
Currently, the only browser supporting VBScript is Microsoft's Internet Explorer 3.0 Beta. For the latest updates to the Internet Explorer and for VBScript examples, see Microsoft's Web site (http://www.microsoft.com), the Visual Basic Script page from Microsoft (http://www.microsoft.com/vbscript/), and the Internet Explorer page at (http://www.microsoft.com/ie/).
Listing B.1 provides the HTML for a quick and dirty Hello World VBScript application. If you have Internet Explorer 3, you can enter this into Notepad or load it from the CD-ROM included with this book.
Listing B.1. The Hello World VBScript Example
<HTML> <HEAD><TITLE>VBScript Hello World Example</TITLE></HEAD> <BODY> <CENTER><H2>Hello World Example</H2> <INPUT TYPE=TEXT VALUE="Insert Name Here" NAME="txtName"><p> <INPUT TYPE=BUTTON VALUE="Click Here" NAME="BtnHello"> </CENTER> <SCRIPT LANGUAGE="VBScript"> <!-- Sub BtnHello_OnClick MsgBox "Hello, world!", 0, "From " + txtName.Value End Sub --> </SCRIPT> </BODY></HTML>
The HTML in Listing B.1 is pretty straight-forward. The first <INPUT> tag defines a text box with an initial value of Insert Name Here and a Name of txtName. The second <INPUT> tag defines a button named BtnHello that has a caption reading Click Here (a button's caption is defined by the Value element of the <INPUT> tag). The page produced is shown in Figure B.1.
Figure B.1. The Hello World VBScript Web page.
The VBScript portion begins with the <SCRIPT> tag. Note that outside of the <SCRIPT> tags, the actual script is placed within an HTML comment (<!-- ... -->). This is to prevent Web browsers which are not script-compliant from attempting to interpret or display the code that is contained within the script.
The Sub defined in the script is the event procedure that will be executed whenever BtnHello is clicked. This concept is identical to the event driven nature of Visual Basic. The procedure executes the MsgBox statement. The text in the message box is Hello, world! and the title for the message box is the string From concatenated with the text contained in the txtName textbox.
A simple example, I know, but it demonstrates the ease-of-use and integration with HTML that VBScript provides.