vbscript
Short Description
Download vbscript...
Description
• Introduction to VBScript • Adding VBScript Code to an HTML Page
VB Script Basics - VBScript Data Types VBScript Variables - VBScript Constants • VBScript Operators – mathematicalcomparison-logical • Using Conditional Statements • Looping Through Code •
Gomathi
1
• VBScript Procedures • – type casting variables • - math functions • –date functions – string functions –other functions • - VBScript Coding Conventions Dictionary Object in VBScript • - Err Object Gomathi
2
• VBScript Procedures • – type casting variables • - math functions • –date functions – string functions –other functions • - VBScript Coding Conventions Dictionary Object in VBScript • - Err Object Gomathi
2
Why VBScript • Visual Basic is compiled Language whereas VBScript is interpreted language. language. • VBScript is used to create Active Server
pages(ASPs), to create administration scripts for windows 95/98 NT,to extend or enhance the functionality of the Microsoft products like word and Excel.
Gomathi
3
• It can also be used as client side scripting language for Internet Explorer. • Netscape does not support VBScript as a client side scripting language.
Gomathi
4
Adding VBScript Code to an HTML Page • You can use the SCRIPT element to add VBScript code to an HTML page. • The Tag • VBScript code is written within paired tags. • For example, a procedure to test a delivery date might appear as follows:
Gomathi
5
• • 2 End Function --> Gomathi
6
• Beginning and ending tags surround the code. • The LANGUAGE attribute indicates the scripting language. • You must specify the language because browsers can use other scripting languages.
Gomathi
7
• Notice that the CanDeliver function is embedded in comment tags (). • This prevents browsers that don't understand the tag from displaying the code. • Since the example is a general function — it is not tied to any particular form control — you can include it in the HEAD section of the page Gomathi
8
• • • • • • • • • • • •
Place Your Order 2 End Function --> Gomathi
9
• You can use SCRIPT blocks anywhere in an HTML page. • You can put them in both the BODY and HEAD sections. • However, you will probably want to put all general-purpose scripting code in the HEAD section in order to keep all the code together. Keeping your code in the HEAD section ensures that all code is read and decoded before it is called from within the BODY section Gomathi
10
• One notable exception to this rule is that you may want to provide inline scripting code within forms to respond to the events of objects in your form. • For example, you can embed scripting code to respond to a button click in a form:
Gomathi
11
• • • • • • • • • • • • •
Test Button Events MsgBox "Button Pressed!" Gomathi
12
• Most of your code will appear in either Sub or Function procedures and will be called only when specified by your code. However, you can write VBScript code outside procedures, but still within a SCRIPT block. • This code is executed only once, when the HTML page loads. This allows you to initialize data or dynamically change the look of your Web page when it loads Gomathi
13
VBScript Data types • VBScript has only one data type called a variant. • A variant is a special kind of data type that can contain different kinds of information, depending on how it is used. • Variant is the only data type which functions return.
Gomathi
14
• At its simplest, a Variant can contain either numeric or string information. • A Variant behaves as a number when you use it in a numeric context and as a string when you use it in a string context.. • Similarly, if you're working with data that can only be string data, VBScript treats it as string data. Of course, you can always make numbers behave as strings by enclosing them in quotation marks (" "). Gomathi
15
Beyond the simple numeric or string classifications, a Variant can make further distinctions about the specific nature of numeric information. For example, you can have numeric information that represents a date or a time. When used with other date or time data, the result is always expressed as a date or a time. Gomathi
16
Gomathi
17
• 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
Gomathi
18
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. Gomathi
19
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". Gomathi
20
• 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. Gomathi
21
• 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. Gomathi
22
• 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".
Gomathi
23
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. Gomathi
24
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)=― Gomathi" names(1)=―swetha‖ • names(2)="Shreeya" Gomathi
25
VBScript Constants • A constant is a meaningful name that takes the place of a number or string and never changes. • Creating Constants • We can create user-defined constants in VBscript using the Const Statement. • Const MyString=―Good day‖ • Const MyAge=―99‖ Gomathi
26
VBScript operators • VBScript has full range of operators including arithmetic operators, comparision operators, concatenation operators and logical operators. • Operator Precedence • When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence. • We can use parentheses to override the order of precedence and force some parts of an expression to be evaluated before others. • Operations within parentheses are always performed before those outside. Within parentheses, however, standard operator precedence is maintained. Gomathi
27
• When expressions contain operators from more than one category, arithmetic operators are evaluated first, comparison operators are evaluated next, and logical operators are evaluated last. Comparison operators all have equal precedence; that is, they are evaluated in the left-to-right order in which they appear. • Arithmetic and logical operators are evaluated in the following order of precedence Gomathi
28
Gomathi
29
• When multiplication and division occur together in an expression, each operation is evaluated as it occurs from left to right. • Likewise, when addition and subtraction occur together in an expression, each operation is evaluated in order of appearance from left to right.
Gomathi
30
• The string concatenation (&) operator is not an arithmetic operator, but in precedence it does fall after all arithmetic operators and before all comparison operators. • The Is operator is an object reference comparison operator. It does not compare objects or their values; it checks only to determine if two object references refer to the same object Gomathi
31
VBScript Conditional statements • Conditional statements are used to perform different actions for different decisions. • In VBScript we have four conditional statements: • If statement - executes a set of code when a condition is true • If...Then...Else statement - select one of two sets of lines to execute • If...Then...ElseIf statement - select one of many sets of lines to execute • Select Case statement - select one of many sets of lines to execute Gomathi
32
• If...Then...Else • Use the If...Then...Else statement if you want to • execute some code if a condition is true • select one of two blocks of code to execute • If you want to execute only one statement when a condition is true, you can write the code on one line: • If i=10 Then alert("Hello")There is no ..Else.. in this syntax. Gomathi
33
Gomathi
34
Gomathi
35
• Select Case • You can also use the "Select Case" statement if you want to select one of many blocks of code to execute
Gomathi
36
Gomathi
37
• This is how it works: First we have a single expression (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each Case in the structure. If there is a match, the block of code associated with that Case is executed
Gomathi
38
VBScript Looping • Looping Statements • Looping statements are used to run the same block of code a specified number of times. • In VBScript we have four looping statements: • For...Next statement - runs code a specified number of times • For Each...Next statement - runs code for each item in a collection or each element of an array
Gomathi
39
• Do...Loop statement - loops while or until a condition is true • While...Wend statement - Do not use it use the Do...Loop statement instead
Gomathi
40
You're Reading a Preview Unlock full access with a free trial.
Download With Free Trial
You're Reading a Preview Unlock full access with a free trial.
Download With Free Trial
The Step Keyword • With the Step keyword, you can increase or decrease the counter variable by the value you specify. • In the example below, the counter variable ( i) is INCREASED by two, each time the loop repeats. • For i=2 To 10 Step 2 some code Next Gomathi
43
• To decrease the counter variable, you must use a negative Step value. You must specify an end value that is less than the start value. • In the example below, the counter variable ( i) is DECREASED by two, each time the loop repeats. • For i=10 To 2 Step -2 some code Next Gomathi
44
• Exit a For...Next • You can exit a For...Next statement with the Exit For keyword. • For i=1 To 10 If i=5 Then Exit For some code Next
Gomathi
45
For Each...Next Loop • • •
A For Each...Next loop repeats a block of code for each item in a collection, or for each element of an array. Example Dim cars(2) cars(0)="Volvo" cars(1)="Saab" cars(2)="BMW" For Each x In cars document.write(x & "") Next
Gomathi
46
• Do...Loop • If you don't know how many repetitions you want, use a Do...Loop statement. • The Do...Loop statement repeats a block of code while a condition is true, or until a condition becomes true. • Repeat Code While a Condition is True • You use the While keyword to check a condition in a Do...Loop statement. • Do While i>10 some code Loop Gomathi
47
You're Reading a Preview Unlock full access with a free trial.
Download With Free Trial
• Do Until i=10 some code Loop If i equals 10, the code inside the loop will never be executed. • Do some code Loop Until i=10 The code inside this loop will be executed at least one time, even if i is equal to 10 Gomathi
49
• Exit a Do...Loop • You can exit a Do...Loop statement with the Exit Do keyword. • Do Until i=10 i=i-1 If i
View more...
Comments