Vbscript
Vbscript
pt 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
Why VBScript
Visual Basic is compiled Language whereas VBScript is interpreted 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
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
<SCRIPT LANGUAGE="VBScript"> <!-Function CanDeliver(Dt) CanDeliver = (CDate(Dt) - Now()) > 2 End Function --> </SCRIPT>
Gomathi 6
Beginning and ending <SCRIPT> tags surround the code. The LANGUAGE attribute indicates the scripting language. You must specify the language because browsers can use other scripting languages.
Gomathi
Notice that the CanDeliver function is embedded in comment tags (<!-- and -->). This prevents browsers that don't understand the <SCRIPT> 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
<HTML> <HEAD> <TITLE>Place Your Order</TITLE> <SCRIPT LANGUAGE="VBScript"> <!-Function CanDeliver(Dt) CanDeliver = (CDate(Dt) - Now()) > 2 End Function --> </SCRIPT> </HEAD> <BODY>
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
<HTML> <HEAD> <TITLE>Test Button Events</TITLE> </HEAD> <BODY> <FORM NAME="Form1"> <INPUT TYPE="Button" NAME="Button1" VALUE="Click"> <SCRIPT FOR="Button1" EVENT="onClick" LANGUAGE="VBScript"> MsgBox "Button Pressed!" </SCRIPT> </FORM> </BODY> </HTML>
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
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
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 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
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
For...Next Loop Use the For...Next statement to run a block of code a specified number of times. The For statement specifies the counter variable (i), and its start and end values. The Next statement increases the counter variable (i) by one.
Gomathi
41
Gomathi
42
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
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
If i equals 9, the code inside the loop above will never be executed. Do some code Loop While i>10 The code inside this loop will be executed at least one time, even if i is less than 10. Repeat Code Until a Condition Becomes True You d to check a condition in a Do...Loop statement.
Gomathi 48
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<10 Then Exit Do Loop The code inside this loop will be executed as long as i is different from 10, and as long as i is greater than 10.
Gomathi 50
VBScript procedures
VBScript has two kinds procedures: Sub procedure Function procedure
Gomathi
51
Sub mysub() some statements End Sub or Sub mysub(argument1,argument2) some statements End Sub
Gomathi
53
Gomathi
54
Function myfunction() some statements myfunction=some value End Function or Function Myfunction(argument1,argument2) some statements myfunction=some value End Function
Gomathi 56
Gomathi
57
Gomathi
58
Example (IE Only) Call a procedure when the user clicks on a button: <body> <button onclick="myfunction()">Click me</button> </body>
Gomathi
59
Procedures can be used to get a variable value: carname=findname()Here you call a Function called "findname", the Function returns a value that will be stored in the variable "carname".
Gomathi
60
Function procedures can calculate the sum of two arguments: Example (IE Only) Function myfunction(a,b) myfunction=a+b End Function
document.write(myfunction(5,9))
Gomathi 61
The function "myfunction" will return the sum of argument "a" and argument "b". In this case 14. When you call a procedure you can use the Call statement, like this: Call MyProc(argument)Or, you can omit the Call statement, like this: MyProc argument
Gomathi 62
VBScript Functions
This page contains all the built-in VBScript functions. The page is divided into following sections: Date/Time functions Conversion functions Format functions Math functions Array functions String functions Other functions
Gomathi
63
Gomathi
64
Gomathi
65
Gomathi
66
Function
Format Functions
Description
Top
Returns an expression formatted as a currency value Returns an expression formatted as a date or time Returns an expression formatted as a number Returns an expression formatted as a percentage
Gomathi
67
Gomathi
68
Gomathi
69
Gomathi
70
Gomathi
71
Gomathi
72
The main reason for using a consistent set of coding conventions is to standardize the structure and coding style of a script or set of scripts so that you and others can easily read and understand the code. Using good coding conventions results in precise, readable, and unambiguous source code that is consistent with other language conventions and as intuitive as possible
Gomathi 74
While this is still an acceptable way to indentify your constants, you may want to use an alternative naming scheme, now that you can create true constants using the Const statement. This convention uses a mixed-case format in which constant names have a "con" prefix. For example: conYourOwnConstant
Gomathi 76
Gomathi
77
Variable Scope
Variables should always be defined with the smallest scope possible. VBScript variables can have the following scope.
Gomathi
78
Gomathi
79
Gomathi
80
For frequently used or long terms, standard abbreviations are recommended to help keep name length reasonable. In general, variable names greater than 32 characters can be difficult to read. When using abbreviations, make sure they are consistent throughout the entire script. For example, randomly switching between Cnt and Count within a script or set of scripts may lead to confusion.
Gomathi 81
Gomathi
82
Gomathi
83
Gomathi
84
Gomathi
85
Gomathi
86
Gomathi
87
Gomathi
88
Gomathi
89
Gomathi
90
Gomathi
91