|Introduction to Web Design - 14. VBScripts|
VBScript (Visual Basic Script) is another scripting language that can be used in the WWW.
However, VBScript is based on MicroSoft Visual Basic and only MicroSoft Internet Explorer
has a VBScript interpreter. Therefore, VBScript will not be practical for many WWW purposes.
It can be used in intranets if everyone is restricted to using Internet Explorer to access the intranet.
It can also be used with Microsoft Servers to create Active Server Pages which use server-side
scripts that create content that is sent to the client’s browser. In that case, the client’s browser is not
restricted to Internet Explorer, as long as the content being sent is consistent for any WWW browser.
Putting the actual script code inside HTML comments is considered good practice with VBScript as it is
All variables in VBScript are of type variant. This data type can store different data types such as strings, integers, floating point numbers, boolean, etc.
in VBScript declares an array of six elements numbered from zero to 5.
The logical operators in VBScript are:
14.3 VBScript Control StructuresSince VBScript does not use end-of-statement characters and curly braces to indicate “groups” of statements, the control structures in VBScript end with one or more keywords such as “End If”, “Wend” (for While End), “End Select”, etc.
14.3.1 The If StatementHere are two examples of VBScript’s if statement:
(1) If s = x Then sum = sum + s End If (2) If s = 0 Then sum = 0 Else sum = sum + s End If
Select Case choice Case 1 Call MsgBox(“You chose Door #1.”) Case 2 Call MsgBox(“You chose Door #2.”) Case 3 Call MsgBox(“You chose Door #3.”) Case Else Call MsgBox(“You chose to keep the $200.”) End Select
x = 6 For y = 1 To x Step 1 x = x - 1 Next
14.5 VBScript Math, Variant, String and Formatting FunctionsVBScript furnishes a number of functions for common math functions such as absolute value abs(x), sine, cosine, logarithms, square root, etc.
VBScript also furnishes a number of functions to determine the data type is currently stored in a variable. These functions include IsArray, IsDate, IsEmpty, and IsNumeric.
VBScript also furnishes a number of formatting functions for output. These include functions for currency, dates and percents.
Call MsgBox(“The input was not a number”, , “Error”)
will create a dialog box with the first string displayed inside the box, “Error” in the titlebar and an “OK” button for the user to dispatch the message box. Note that even though the call above does not include the second, optional parameter, a blank space separated from the other parameters by a comma is required.
firstname = InputBox(“Enter your first name”, “Input”, , 700, 700)
will place the input box approximately one half inch down and one half inch to the right of the top left corner of the screen. Note that this function call does not include the word “Call”. Note also that this function returns the value entered by the user and this value is assigned to the variable “firstname”.
14.7 Defining Your Own Functions in VBScript - An ExampleVBScript has two means of grouping statements that perform a function together. One is to define a subroutine and the other is to define a function. The main difference is that functions return a value and subroutines do not. Both functions and subroutines can take parameters. Functions return values by assigning a value to the name of the function.
The HTML file below contains a VBScript that defines a function called “WhichLarger” and a subroutine called “GetNums_OnClick”. Note that the function has assignment statements to the name of the function. Note that when the function is called in the subroutine GetNums_OnClick, the value returned by the function is assigned to a variable called “larger”.
The subroutine “GetNums_OnClick” is called when the button called “GetNums” in the form is clicked. Thus, the click event calls the subroutine.
<html> <head> <title>A Simple VBScript</title> <script type = "text/vbscript"> <!-- Option Explicit Dim intTotal, num1, num2 Function WhichLarger(x, y) If(x > y) Then WhichLarger = x Else WhichLarger = y End If End Function Sub GetNums_OnClick() Dim larger, sum num1 = InputBox("Enter an integer", "Input Box", , 700, 700) num2 = InputBox("Enter an integer", "Input Box", , 3200, 3200) larger = WhichLarger(num1, num2) sum = CInt(num1) + CInt(num2) Call MsgBox("The sum of the two numbers is " & sum & ". " &_ larger & " was the larger of the two numbers.") End Sub --> </script> </head> <body> Click the button to see what happens. <form> <input name = "GetNums" type = "button" value = "Click Here" /> </form> </body> </html>
Cynthia J. Martincic
Saint Vincent College
Latrobe, PA 15650