VBScript
VBScript is a Microsoft scripting language.
VBScript is the default scripting language in ASP.
Client-side VBScript only works in Internet Explorer!!!
VBScript Editor
With our online editor, you can edit the VBScript code, and click on a button to view the result.
| Example (IE Only)
|
What is VBScript?
- VBScript is a scripting language
- A scripting language is a lightweight programming language
- VBScript is a light version of Microsoft's programming language Visual Basic
- VBScript is only supported by Microsoft's browsers (Internet Explorer)
How Does it Work?
When a VBScript is inserted into an HTML document, Internet Explorer browser will read the HTML and interpret the VBScript. The VBScript can be executed immediately, or at a later event.VBScript only works in Microsoft browsers (Internet Explorer).
VBScript Where To...
| | |
VBScripts can be placed in the body and in the head section of an HTML document.
Where to Put the VBScript
VBScripts in a page will be executed immediately while the page loads into the browser. This is not always what we want. Sometimes we want to execute a script when a page loads, or at a later event, such as when a user clicks a button. When this is the case we put the script inside a function or a sub procedure, you will learn about procedures in a later chapter.Scripts in <head>
Put your functions and sub procedures in the head section, this way they are all in one place, and they do not interfere with page content. Example (IE Only)
|
Scripts in <body>
If you don't want your script to be placed inside a function, and especially if your script should write page content, it should be placed in the body section. Example (IE Only)
|
Scripts in <head> and <body>
You can place an unlimited number of scripts in your document, and you can have scripts in both the body and the head section. Example (IE Only)
|
VBScript Variables
As with algebra, VBScript variables are used to hold values or expressions.
A variable can have a short name, like x, or a more descriptive name, like carname.
Rules for VBScript variable names:
- Must begin with a letter
- Cannot contain a period (.)
- Cannot exceed 255 characters
In VBScript, all variables are of type variant, that can store different types of data.
Declaring (Creating) VBScript Variables
Creating variables in VBScript is most often referred to as "declaring" variables.
You can declare VBScript variables with the Dim, Public or the Private statement. Like this:
| Dim x Dim carname |
Now you have created two variables. The name of the variables are "x" and "carname".
You can also declare variables by using its name in a script. Like this:
| carname="Volvo" |
Now you have also created a variable. The name of the variable is "carname". However, this method is not a good practice, because you can misspell the variable name later in your script, and that can cause strange results when your script is running.
If you misspell for example the "carname" variable to "carnime", the script will automatically create a new variable called "carnime". To prevent your script from doing this, you can use the Option Explicit statement. This statement forces you to declare all your variables with the dim, public or private statement.
Put the Option Explicit statement on the top of your script. Like this:
| Option Explicit Dim carname carname=some value |
Assigning Values to Variables
You assign a value to a variable like this:
| carname="Volvo" x=10 |
The variable name is on the left side of the expression and the value you want to assign to the variable is on the right. Now the variable "carname" has the value of "Volvo", and the variable "x" has the value of "10".
Lifetime of Variables
How long a variable exists is its lifetime.
When you declare a variable within a procedure, the variable can only be accessed within that procedure. When the procedure exits, the variable is destroyed. These variables are called local variables. You can have local variables with the same name in different procedures, because each is recognized only by the procedure in which it is declared.
If you declare a variable outside a procedure, all the procedures on your page can access it. The lifetime of these variables starts when they are declared, and ends when the page is closed.
VBScript Array Variables
An array variable is used to store multiple values in a single variable.
In the following example, an array containing 3 elements is declared:
| Dim names(2) |
The number shown in the parentheses is 2. We start at zero so this array contains 3 elements. This is a fixed-size array. You assign data to each of the elements of the array like this:
| names(0)="Tove" names(1)="Jani" names(2)="Stale" |
Similarly, the data can be retrieved from any element using the index of the particular array element you want. Like this:
| mother=names(0) |
You can have up to 60 dimensions in an array. Multiple dimensions are declared by separating the numbers in the parentheses with commas. Here we have a two-dimensional array consisting of 5 rows and 7 columns:
| Dim table(4,6) |
Asign data to a two-dimensional array:
| Example (IE Only)
|
VBScript Conditional Statements
Conditional StatementsConditional statements are used to perform different actions for different decisions.In VBScript we have four conditional statements:
If...Then...ElseUse the If...Then...Else statement if you want to
If you want to execute more than one statement when a condition is true, you must put each statement on separate lines, and end the statement with the keyword "End If":
If you want to execute a statement if a condition is true and execute another statement if the condition is not true, you must add the "Else" keyword:
If...Then...ElseIfYou can use the If...Then...ElseIf statement if you want to select one of many blocks of code to execute:
| |