Adding VBScript Code to an HTML Page

VBScript can be embedded in HTML pages either to create dynamic pages
(client side scripting) and CGI-based applications, you may take advantage
of Microsoft's Active Server Pages (ASP) technology.

To embed VBScript in a web page for "client-side" scripting you
would enclose the code within the opening and closing script tags: <script
language="VBScript"> and </script>

To embed VBScript in a web page for "serever-side"
scripting you enclose the commands
within an opening <% symbol and a closing %>
symbol. This is known as a SCRIPT element and is used when creating Active
Server Pages.

ASPs are based on VBScript, Microsoft's development language. To
create and ASP you need to create a HTML file with script elements and the
extension ".asp".

ASP files are processed by the server prior to sending the page to the
browser, which makes it very easy to create dynamic pages.

Note: While VBScript can be used for both client-side scripting and
server-side
scripting, for client-side scripting JavaScript is more universally
supported. Nevertheless, using VBScript for server-side scripting, as described
here, is very powerful and compatible with any browser.

VB Script Variables

A variable is a convenient placeholder that refers to a computer memory
location where you can store program information that may change during the
time your script is running.

You only have to refer to a variable by name to see its value or to change
its value. In VBScript, variables are always of one fundamental data type, Variant.

Variant

A Variant is a special kind of data type that can contain different kinds
of information, depending on how it's used.

At its simplest, a Variant can contain either numeric or string
information.

If you're working with data that looks like numbers, VBScript
assumes that it is numbers and does the thing that is most appropriate
for numbers.

If you're working with data that can only be string data,
VBScript treats it as string data.

Variant Subtypes

Beyond the simple numeric or string classifications, a Variant can make
further distinctions about the specific nature of numeric information.
(It knows what it is).

You can use conversion functions to convert data from one subtype to another. In addition, the
VarType
function returns information about how your data is stored within a Variant.

It is recommended you use the same naming conventions objects, variables, and procedures
as in Visual Basic. That is each variable of a certain type is preceded
by 3 letters describing its Variant Subtype.

The following table shows the subtypes of data that a Variant can
contain and the 3 letter naming convention for each sub type.

Subtype

Naming
Convention

Description

Empty

Variant is uninitialized. Value is 0 for numeric variables or a
zero-length string ("") for string variables.

Null

Variant intentionally contains no valid data.

Boolean

bln

Contains either True
or False.

Byte

byt

Contains integer in the range 0 to 255.

Integer

int

Contains integer in the range -32,768 to 32,767.

Currency

cur

-922,337,203,685,477.5808 to 922,337,203,685,477.5807.

Long

lng

Contains integer in the range -2,147,483,648 to 2,147,483,647.

Single

sng

Contains a single-precision, floating-point numbers

Double

dbl

Contains a double-precision, floating-point numbers.

Date (Time)

dtm

Contains a date between 1/1/ 100 & 12/31/ 9999.

String

str

Contains a variable-length string up to 2
billion characters long.

Object

obj

Contains an object.

Error

err

Contains an error number.

Declaring Variables

You declare variables using the Dim
statement, the Public
statement, and the Private statement:

Dim intDegreesFahrenheit

You declare multiple variables by separating each variable name with a
comma:

Dim intTop, intBottom, intLeft, intRight

You can also declare a variable implicitly by simply using its name in
your script. That's not generally a good practice because you could misspell
the variable name in one or more places, causing unexpected results when
your script is run.

For that reason, the Option Explicit statement is
available to require explicit declaration of all variables.

As with regular Visual Basic The Option Explicit
statement should be the first statement in your script.

Scope and Lifetime of Variables

When you declare a
variable within a procedure, only code within that procedure can access or
change the value of that variable. It has local scope
and is called a procedure-level
variable.

If you declare a variable outside a procedure, you make it
recognizable to all the procedures in your script. This is a script-level
variable, and it has script-level scope.

How long a variable exists is its lifetime.

The lifetime of a script-level
variable extends from the time it's declared until the time the script is
finished running.

At procedure level, a variable exists only as long as you
are in the procedure. When the procedure exits, the variable is destroyed.

Local variables are ideal as temporary storage space when a procedure is
executing. You can have local variables of the same name in several different
procedures because each is recognized only by the procedure in which it is
declared.

Assigning Values to Variables

Values are assigned to variables creating an expression as follows: the
variable is on the left side of the expression and the value you want to
assign to the variable is on the right. For example:

intB = 200

Scalar Variables and Array Variables

A variable containing a single value is a scalar variable & A variable that can contain a series of values is called an array variable.

Array variables are declared using parentheses ( ) following the variable name.

Below, an array containing 11 elements is declared:

Dim intA(10)

Although the number shown in the parentheses is 10, all arrays
in VBScript are zero-based. This array actually contains 11 elements
(0 to 10).

Array Assignment

You assign data to each of the elements of the array using the array
index. Begin at zero and end at 10:

intA(0) = 256
intA(1) = 324
. . .
intA(10) = 55

Similarly, the data can be retrieved from any element using the index of the array element you want. For example:

intSomeVariable = intA(8)

Dynamic Arrays

You can also declare an array whose size changes during the time your
script is running. This is called a dynamic array.

The array is initially
declared using either Dim or ReDim. However, for a dynamic array, no size or number of
dimensions is
placed inside the parentheses:

Dim intMyArray()
ReDim intAnotherArray()

To use a dynamic array, you must subsequently use ReDim to
specify the
number of dimensions and the size of each dimension. The Preserve keyword
is used with ReDim to preserve
the contents of the array as the resizing takes place.

ReDim intMyArray(25)
. . .
ReDim Preserve intMyArray(30)

There is no limit to the number of times you can resize a dynamic array, but if you make an array smaller than it was, you lose the
data in the eliminated elements.

Constant

A constant
is a meaningful name that takes the place of a number or string and never
changes.

You create user-defined constants in VBScript using the Const
statement. Using the Const statement, you can create string or numeric
constants with meaningful names and assign them literal values. For example:

Const conMyString = "This is my string."
Const conMyAge = 49

The string literal is enclosed in quotation marks (" "). Date
literals and time literals are represented by enclosing them in number
signs (#). For example:

Select Case

The Select Case structure works with a single test expression that is
evaluated once. The result of the expression is
then compared with the values for each Case in the structure. If there
is a match, the block of statements associated with that Case is
executed:

VB Repetition Structures

Looping allows you to run a group of statements repeatedly. Some loops repeat
statements until a condition is False; others repeat statements until a
condition is True. There are also loops that repeat statements a
specific number of times.

Some looping statements available in VBScript are:

Do...Loop:
Loops While or Until a condition is True.

For...Next:
Uses a counter to run statements a specified number of times.

Do Loops

Do...Loop statements run an
indefinite number of times.

Use the While keyword to check a condition in a Do...Loop
statement. The loop runs While a condition is True.

Use the Until keyword to check a condition in a Do...Loop
statement. When
condition is False, the looping occurs.

Using Sub and Function Procedures in Code

A Function in your code must always be used on the right side of a
variable assignment or in an expression:

Temp = Celsius(fDeg)

or

MsgBox "Temperature=" & Celsius(fDeg) & " C."

To call a Sub procedure from another procedure, you can just type the name of the procedure along with values for any required arguments, each separated by a comma.

The Call statement is not required, but if you do use it, you must enclose any arguments in parentheses.

Call MySub(firstarg, secondarg)
MySub firstarg, secondarg

or
Call ConvertTemp()
ConvertTemp

Notice that the parentheses are omitted in the call when the Call statement isn't used.

VBScript with Form Elements

Like JavaScript, VBScript can be used to read data from form elements or
write data to form elements.

To access a textbox in a form use the following when you are referencing a
textbox with the name txtName in the first form int he document - You can
not use the form name to reference the form like you did in JavaScript:

Document.Forms(0).txtName.Value

To convert the String retrieved from the textbox to a number you need to
use:

CSng for Singles, CDbl for Doubles, CInt for Integers,
CLng for
Longs (etc.)