1. Introduction to VBScript
VBScript is a scripting language and the default language of ASP. You can directly embed VBScript in HTML files to expand HTML, making it more than just a page format language. Web pages with VBScript scripts can respond to user operations.
2. Add VBScript code to the HTML page
VBSript scripts exist as part of HTML documents. That is to say, these codes can be executed only when they are stored in HTML documents. Script code can be placed anywhere in the HTML document, that is, the BODY or HEAD part. Generally, the script code is concentrated in the HEAD part to ensure that all script code is read and decoded before the BODY part calls the code.
When adding VBScript code to the HTML page, it should start with the <script> mark and end with the </script> mark. The basic syntax format is as follows:
<Script syntax Ge = "script language name" [event = "event name"] [For = "Object Name"]>
<! --
Script code
-->
</Script>
The SCRIPT tag has the following three attributes.
(1) Language: Specifies the script Language used by the script code. For Internet Explorer, the value of this attribute can be "VBScrip" or "JScript". The former can be abbreviated as "VBS ".
(2) EVENT: Specifies the EVENT associated with the script code.
(3) FOR: Specifies the object associated with the event.
In the preceding syntax, the script code is embedded in the annotation mark! -- And --> are used to prevent browsers that cannot recognize the SCRIPT tag from displaying code on the page.
Note:
The SCRIPT mark can appear at any time in the HEAD and BODY of the HTML document.
Most of the script code is placed in the Sub or Functip process. This type of code is executed only once when the HTML page is loaded. You can initialize the data when loading the Web page or dynamically change the page appearance.
VBScript Data Type
VBScript has only one data type, namely, Variant, which is called a Variant. Varriant is a special data type. It can contain different types of information according to the method used. Because Variant is the only data type in VBScript, it is also the data type returned by all functions in VBScript.
The simplest Variant can contain numbers or strings. Variant is used as a number in the numeric context and as a string in the string context. That is to say, if you use data that looks like a number, VBScript assumes it is a number and processes it in a numeric way. Similarly, if the data used can only be a string, VBScript will process it by string. You can also enclose a number in quotation marks ("") to make it a string.
The type of the value information contained in Variant is called the Child type. In most cases, you can put the required data into Variant, and Variant will operate in the most suitable way.
Data subtypes contained by Variant
| Child type |
Description |
| Boolean |
Contains True or False |
| Byte |
Contains an integer between 0 and 255. |
| Currency |
-922,337,203,685,477.5808 to 922,337,203,685,477.5808 |
| Long |
Contains an integer ranging from-2,147,483,648 to 2,147,483,648. |
| Single |
Contains a single-precision floating point number. The negative value ranges from-3.402823E38 to-1.401298E-45, and the positive value ranges from 1.401298E-45 to 3.402823E38. |
| Double |
Contains a double-precision floating point number. The negative value ranges from-1.79769313486232E308 to-4.94065645841247E-324. The positive value ranges from 4.94065645841247E-324 to 1.79769313486232e |
| Date (Time) |
Contains the number indicating the date. The date range is from January 1, January 1, 100 AD to January 1, December 9999 AD. |
| String |
Contains a variable-length string, up to 2 billion characters |
| Object |
Contains objects |
| Error |
Contains error code |
| Integer |
Contains an integer between 32,768 and 32,767. |
VBScript variables and constants
A variable is a convenient placeholder used to reference the computer memory address, which can store information about programs that can be changed during script running. For example, you can create a variable named ClickCount to store the number of times a user clicks an object on the web page. When using a variable, you do not need to know the address of the variable in the computer memory. You only need to reference the variable through the variable name to view or more required variable values. In VBScript, there is only one basic data type, namely Variant. Therefore, the Data Types of all variables are Variant.
1. variable naming rules
Variable naming must follow the standard naming rules of VBScript:
The first character must be a letter. Cannot contain the embedded period (.). It cannot exceed 255 characters. It must be unique within the declared scope. The keyword cannot be the same as that of VBScript.
2. Declare Variables
In VBScript, Dim statements, Public statements, and Private statements are usually used to explicitly declare variables and allocate storage space. The syntax format is:
{Dim | Private | Public} <variable name 1> [, <variable name 2>] [, <variable name 3>]... [, <variable name>]
Note:
(1) variables declared by the Public statement can be used in all processes in all scripts;
(2) The Script-level variables declared with Dim can be used in all the processes in the Script, but the process variables can only be used in the process;
(3) variables declared by the Private statement can only be used in scripts that declare the variable.
(4) When multiple variables are declared, use commas to separate the variables. For example: Dim a, B, c, d
Another way is to implicitly declare a variable by directly using the variable name in the script. This is usually not a good habit, because sometimes unexpected results occur when running the script Due to misspelling of the variable name. Therefore, it is best to use the Option Explicit statement to declare all variables and use them as the first statement of the script. (That is, it must appear before any HTML identifier or other VBScript commands. Otherwise, this statement is considered invalid.
3. Scope and survival of Variables
The scope of a variable is determined by its position. If a variable is declared during the process, only the code in the process can access or change the value. At this time, the variable has a local scope and is called a process-level variable. If a variable is declared outside the process, the variable can be identified by all processes in the script. It is called a script-level variable and has a script-level scope.
The time when a variable exists is called the retention period. The script-level variable retention period starts from the declared moment until the script operation ends. For a process-level variable, its survival period is only the time when the process runs. After the process is completed, the variable disappears. Local variables are ideal for temporary storage during execution. Local variables with the same name can be used in different processes because each local variable is identified only by the declared process.
4. assign values to variables
You can use the value assignment statement to assign a value to a variable in the following format:
Variable name = value such as: RserName = "shadow"
Password = "698726as"
BirthDate = #1975-12-25"
5. scalar variables and array Variables
In most cases, you only need to assign a variable value to the declared variable. A variable that contains only one value is called a scalar variable. In some cases, it is more convenient to assign multiple values to a variable. Therefore, you can create a variable that contains a series of values, called an array variable. The declaration of an array variable is similar to that of a scalar variable. The only difference is that when an array variable is declared, the variable name is enclosed by parentheses (). For example, myWeekDay (6) declares a one-dimensional array containing seven elements.
Arrays are not limited to one dimension. The maximum dimension of the array can be 60. When declaring a multi-dimensional array, use commas to separate each number in the brackets that represents the array size. For example, Dim MyTable () declares a two-dimensional array with 6 rows and 10 columns.
6. Dynamic Array
You can also declare a dynamic array, that is, the array that changes when the script is run. The Dim statement or ReDim statement is used for the initial declaration of the dynamic array. To use a dynamic array, the parentheses cannot contain numbers. You must then use ReDim to determine the dimension and the size of each dimension. For example, the following ReDim statement sets the initial size of the dynamic array to 25, and the subsequent ReDim statement re-adjusts the size of the array to 30, at the same time, use the Preserve keyword to retune the size of the array to retain the content.
Dim myArray (25)
......
ReDim AnotherArray (30)
7. Simple Application Example
<Html>
<Head> <title> Array application </title>
<Script language = "VBScript" for = "button1" Event = "onClick">
Dim myWeekDay (6)
Dim myWeek
MyweekDay (0) = "Sunday"
MyweekDay (1) = "Monday"
MyweekDay (2) = "Tuesday"
MyweekDay (3) = "Wednesday"
MyweekDay (4) = "Thursday"
MyweekDay (5) = "Friday"
MyweekDay (6) = "Saturday"
MyWeek = WeekDay (Date)-1
Select Case true
Case myWeek = 1
Msgbox "today is" & myweekDay (1) & ". At the beginning of the week, I worked hard. "
Case myWeek = 2
Msgbox "today is" & myweekDay (2) & ", and you have to work hard. "
Case myWeek = 3
Msgbox "today is" & myweekDay (3) & ", continue to work hard. "
Case myWeek = 4
Msgbox "today is" & myweekDay (4) & ". It will be less than a week in two days. "
Case myWeek = 5
Msgbox "today is" & myweekDay (5) & ", haha, tomorrow is the week of weekday. "
Case myWeek = 6
Msgbox "today is" & myweekDay (6) & ". You can have fun today. "
Case myWeek = 0
Msgbox "today is" & myweekDay (0) & ". have a good rest. Tomorrow is the beginning of the new week. "
End select
</Script>
</Head>
<Body>
<Form name = form1>
<Input type = "button" name = "button1" value = "Please check the day of the week.">
</Form>
</Body>
</Html>
VBScript Operator
VBScript operators include Arithmetic Operators, join operators, comparison operators, and logical operators. When an expression contains multiple operators. Each part is calculated in a predetermined order, which is called the computing priority. When using an operation, the operation is performed according to the sequence inside and outside brackets, and the standard operator priority must be followed in the brackets; the priority is calculated in the order from left to right at the same time.
1. Arithmetic Operators
Variable naming must follow the standard naming rules of VBScript:
Arithmetic Operators
| Operator |
Name |
Expression example |
Operator |
Name |
Expression example |
| ^ |
Multiplication party |
A ^ B |
* |
Multiplication |
A * B |
| / |
Floating Point Division |
A/B |
|
Integer Division |
A |
| + |
Addition |
A + B |
- |
Subtraction |
A-B |
| Mod |
Remainder modulo operation |
A Mod B |
- |
Take negative |
-C |
2. Comparison Operators
Comparison
| Operator |
Description |
True if |
False if |
Null if |
| < |
Less |
Expression 1 <expression 2 |
Expression 1> = expression 2 |
Expression 1 or expression 2 = Null |
| <= |
Less than or equal |
Expression 1 <= expression 2 |
Expression 1> Expression 2 |
Expression 1 or expression 2 = Null |
| > |
Greater |
Expression 1> Expression 2 |
Expression 1 <= expression 2 |
Expression 1 or expression 2 = Null |
| > = |
Greater than or equal |
Expression 1> = expression 1 |
Expression 1 <= expression 2 |
Expression 1 or expression 2 = Null |
| = |
Equal |
Expression 1 = expression 2 |
Expression 1 <> Expression 2 |
Expression 1 or expression 2 = Null |
| <> |
Not equal |
Expression <> Expression 2 |
Expression 1 = expression 2 |
Expression 1 or expression 2 = Null |
| Is |
Used for objects |
|
|
Expression 1 or expression 2 = Null |
3. logical operators
| Operator |
Name |
Example |
Description |
| And |
And |
(4> 5) And (3 <4) |
The value is False. The two expressions are true and the result is true. |
| Or |
Or |
(4> 5) or (3 <4) |
The value is True. If one of the two expressions is True, the result is True. Only when the values of the two expressions are false can the result be false. |
| Not |
Non |
Not (1> 0) |
If the value is False, the "reverse" operation is performed from true to False or from False to true. |
| Xor |
Logic exclusive or |
10> 8Xor8> 6 |
The value is ": True. The values of the two expressions are True or false. The entire expression is True. Otherwise, the expression is false. |
4. Join Operators
Join Operator
| Join Operator |
& |
String connection |
Concatenates two expressions with strings. |
5. Operator priority
Operator priority
| Priority |
Operator type |
Operator |
| 1 |
Arithmetic Operators |
^ (Exponential Operation) |
| 2 |
-(Negative calculation) |
| 3 |
*,/(Multiplication and division) |
| 4 |
(Integer Division) |
| 5 |
Mod (remainder Operation) |
| 6 |
+,-(Addition and subtraction) |
| 7 |
Join Operator |
& (String connection) |
| 8 |
Comparison Operators |
=, <>, <,>, <=,> =, Is |
| 9 |
Logical operators |
Not |
| 10 |
And |
| 11 |
Or |
| 12 |
Xor |