Vbscript

VARSHAKUMARI49 2,485 views 191 slides Sep 13, 2019
Slide 1
Slide 1 of 191
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104
Slide 105
105
Slide 106
106
Slide 107
107
Slide 108
108
Slide 109
109
Slide 110
110
Slide 111
111
Slide 112
112
Slide 113
113
Slide 114
114
Slide 115
115
Slide 116
116
Slide 117
117
Slide 118
118
Slide 119
119
Slide 120
120
Slide 121
121
Slide 122
122
Slide 123
123
Slide 124
124
Slide 125
125
Slide 126
126
Slide 127
127
Slide 128
128
Slide 129
129
Slide 130
130
Slide 131
131
Slide 132
132
Slide 133
133
Slide 134
134
Slide 135
135
Slide 136
136
Slide 137
137
Slide 138
138
Slide 139
139
Slide 140
140
Slide 141
141
Slide 142
142
Slide 143
143
Slide 144
144
Slide 145
145
Slide 146
146
Slide 147
147
Slide 148
148
Slide 149
149
Slide 150
150
Slide 151
151
Slide 152
152
Slide 153
153
Slide 154
154
Slide 155
155
Slide 156
156
Slide 157
157
Slide 158
158
Slide 159
159
Slide 160
160
Slide 161
161
Slide 162
162
Slide 163
163
Slide 164
164
Slide 165
165
Slide 166
166
Slide 167
167
Slide 168
168
Slide 169
169
Slide 170
170
Slide 171
171
Slide 172
172
Slide 173
173
Slide 174
174
Slide 175
175
Slide 176
176
Slide 177
177
Slide 178
178
Slide 179
179
Slide 180
180
Slide 181
181
Slide 182
182
Slide 183
183
Slide 184
184
Slide 185
185
Slide 186
186
Slide 187
187
Slide 188
188
Slide 189
189
Slide 190
190
Slide 191
191

About This Presentation

VBScript fundamentals


Slide Content

VBSCRIPT
-Varsha Kumari

What is VBScript?
•short for Visual Basic Script
•powerful programming language developed
by Microsoft
•scripting language
•a subset of Visual Basic
•intended to be used with Microsoft products -
MS Project and MS Office.

Features of VBScript
•Is a scripting language
•case insensitive
•Simple syntax
•easy to learn and implement
•Can be used at both client and server side.

•Like JavaScript it can be run on the client-side,
through a web browser and like PHP it can
also be run on the server-side on a web server.
•default scripting language for ASP.
•On the client-side we can mix it with HTML to
make our web pages live and interactive.
•VBScript is only supported by the Internet
Explorer

Disadvantages
•Used only by IE browsers, hence javascript is
prefered over vbscript
•Since there is no development environment
available, debugging is difficult

First VBScript Code
•write the VBScript code in a text editor and
create an HTML file and save the same with
.htm or .html extension.
•open the above file in Internet Explorer.

•<html>
<head>
<title>VBScript Hello World!</title>
</head>
<body>
<script type="text/vbscript” >
document.write("Hello World!")
</script>
</body>
</html>
•Output
Hello World!

Working
•<script> tags are actually HTML
•It tell the browser to expect and process the enclosed
text as a script
•text/vbscript specify the language
•document.write("Hello World!") outputs the text "Hello
World!“ -is a method of the document object
•strings / text in VBScript code are enclosed in quotes.

VBScript Basics
•Formatting
1.No statement terminator is used like semicolon
2.VBScript ignores spaces , tabs and newlines that
appears Within the script.
•VBScript Comments:
1.Line starts with the keyword “REM” treated as
comment
2.Line starts with an apostrophe (') not processed
as code by the VBScript interpreter

Using “REM”
<script type="text/vbscript">
REM enter the numbers
REM scanf the numbers
</script>

Using (‘)
•<script type="text/vbscript">
' the following line declares a variable
Dim age
' the following line assings a value to our variable
age
age = 20
' the following line prints the text
document.write(age)
<!—REM comment
REM comment
//->
</script>

Single line & multiline syntax
•Colon(:) act as line seperator
<script type="text/vbscript">
var1 = 10 : var2 = 20
</script>
•underscore (_) at the end of a line indicates
that the following line is also part of the
current line.Eg.
<script type="text/vbscript">
test = 3 + 4_
+ 87
document.write(“test”)
</script>

<html>
<body>
<script language="vbscript“
type="text/vbscript">
var1 = 10: var2 = 20
Sum = var1 + var2
document.write("The Sum of two numbers"&_
"var1 and var2 is " & Sum)
</script>
</body>
</html>

Reserved Words

Reserved Keywords

Adding script to documents
•way to include VBScript in HTML file is as
follows:
1. Script in <head>...</head> section.
2. Script in <body>...</body> section.
3. Script in <body>...</body> and <head>...</head>
sections.
4. Script in an external file and then include in
<head>...</head> section.

VBScript in <head>...</head>
section:
<html>
<head>
<script type="text/Vbscript">
Function sayHello()
Msgbox("Hello World")
End Function
</script>
</head>
<body>
<input type="button" onclick="sayHello()" value="Say Hello" />
</body>
</html>

VBScript in <body>...</body>
section:
<html>
<head>
</head>
<body>
<script type="text/vbscript">
document.write("Hello World")
</script>
<p>This is web page body </p>
</body>
</html>

VBScript in <body> and <head>
sections:
<html>
<head>
<script type="text/vbscript">
Function sayHello() msgbox("Hello World") End Function
</script> </head>
<body> <script type="text/vbscript">
document.write("Hello World“)
</script>
<input type="button" onclick="sayHello()" value="Say
Hello" />
</body>
</html>

VBScript in External File:
First Save filename.vbs file
Function sayHello()
Msgbox "Hello World"
End Function

VBScript in External File:
<html>
<head>
<script type="text/vbscript" src="filename.vbs" >
</script>
</head>
<body> ....... </body>
</html>

VBScript Variables
•variable is a named memory location
•used to hold a value that can be changed
during the script execution.
•Variable Name must begin with an alphabet.
•Variable names cannot exceed 255 characters.
•unique in the declared context
•VBScript has only ONE fundamental data
type, Variant.

Declaring variables
•Variables are declared using “dim” keyword.
•Vbscript allows undeclared variables
•Declaring variables makes code easier to read
and debug later
Dim Var1
Dim Var1,Var2

Assigning Values to the Variables
•Values are assigned on the rhs of “=“
•The numeric values ~ without double quotes
•The String values ~ within doublequotes(")
•Date and Time variables enclosed within hash
symbol(#)
•' Below Example, The value 25 is assigned to
the variable.
Value1 = 25
StrValue = “VBScript”
Date1 = #01/01/2020#
Time1 = #12:30:44 PM#

Scope of the Variables
•Variables can be declared in 3 ways according
to the scope of the variable
1.Dim
2.Public
3.Private

DIM
•Declared at a Procedure level & are available
only within the same procedure.
•Declared at script level are available to all the
procedures within the same script.

<html> <body>
<script type="text/vbscript">
Dim Var1
Dim Var2
Function add()
Var1 = 10
Var2 = 15
Dim Var3
Var3 = Var1+Var2
Document.write(var3)
End Function
Call add()
Msgbox Var1
Msgbox Var2
Msgbox Var3
</script>
</body> </html>

PUBLIC
•Variables declared using "Public" Keyword are
available to all the procedures across all the
associated scripts.
PRIVATE
•Have scope only within that script in which
they are declared.

<html>
<body>
<script language="vbscript" type="text/vbscript">
Dim Var1
Dim Var2
Public Var3
Function add()
Var1 = 10
Var2 = 15
Var3 = Var1+Var2
Document.write(var3)
End Function
Call add()
Msgbox Var1
Msgbox Var2
Msgbox Var3
</script>
</body> </html>

<html><body>
<script language="vbscript" type="text/vbscript">
Dim Var1
Dim Var2
Private Var3
Call add()
Function add()
Var1 = 10
Var2 = 15
Var3 = Var1+Var2
Msgbox Var3
End Function
Msgbox Var1
Msgbox Var2
Msgbox Var3
</script>
</body> </html>

VBScript constants
•named memory location used to hold a value
that CANNOT be changed during the script
execution.
•Declaration:
[Public | Private] Const Constant_Name= Value
•The Use of Public or Private is Optional.

<html>
<body>
<script language="vbscript" type="text/vbscript">
Dim intRadius
intRadius = 20
const pi=3.14
Area = pi*intRadius*intRadius
Msgbox Area
</script>
</body>
</html>

<html>
<body>
<script language="vbscript" type="text/vbscript">
Const myString = "VBScript"
Const myDate = #01/01/2050#
Msgbox myString
Msgbox myDate
</script>
</body>
</html>

<html>
<body>
<script language="vbscript" type="text/vbscript">
Dim intRadius
intRadius = 20
const pi=3.14
pi = pi*pi NOTE error
Area = pi*intRadius*intRadius
Msgbox Area
</script>
</body>
</html>

Operator
•Arithmetic Operators
•Comparison Operators
•Logical (or Relational) Operators
•Concatenation Operators

The Arithmatic Operators

<script type="text/vbscript">
Dim a : a = 5
Dim b : b = 10
Dim c : c = a+b
Document.write ("Addition Result is " &c)
Document.write ("<br></br>“)
</script>

Comparison operators

<html>
<body>
<script language="vbscript" type="text/vbscript">
Dim a ,b,c
a = 10 : b = 20
If a=b Then
Document.write (“values are equal : True")
Document.write ("<br></br>“)
Else
Document.write (" values are not equal : False")
Document.write ("<br></br>")
End If
</body>
</html>

logical operators

Concatenation operators

Decisions
•allows programmers to control the execution
flow of a script or one of its sections

if statement
If(boolean_expression) Then
Statement 1
.....
.....
Statement n
End If

<html>
<body>
<script language="vbscript" type="text/vbscript">
Dim a : a = 20
Dim b : b = 10
If a > b Then
Document.write ("a is Greater than b“)
End If
</script>
</body>
</html>

if…else statement
If(boolean_expression) Then
Statement 1
.....
.....
Statement n
Else
Statement 1
.....
....
Statement n
End If

<html>
<body>
<script language="vbscript" type="text/vbscript">
Dim a : a = 5
Dim b : b = 25
If a > b Then
Document.write "a is Greater"
Else
Document.write "b is Greater"
End If
</script>
</body>
</html>

if…elseif…else statement
If(boolean_expression) Then
Statement
.....
ElseIf (boolean_expression) Then
Statement
…….
ElseIf (boolean_expression) Then
Statement
....
Else
Statement
....
End If

<html>
<body>
<script language="vbscript" type="text/vbscript">
Dim a
a = -5
If a > 0 Then
Document.write "a is a POSITIVE Number"
ElseIf a < 0 Then
Document.write "a is a NEGATIVE Number"
Else
Document.write "a is EQUAL than ZERO"
End If
</script>
</body>
</html>

nested if statements
If(boolean_expression) Then
Statement .....
If(boolean_expression) Then
Statement …
ElseIf (boolean_expression) Then
Statement…
Else
Statement….
End If
Else
Statement …..
End If

<html> <body>
<script language="vbscript" type="text/vbscript">
Dim a :a = 23
If a > 0 Then
Document.write "The Number is a POSITIVE Number"
If a = 1 Then
Document.write "The Number is Neither Prime NOR Composite"
Elseif a = 2 Then
Document.write "The Number is the Only Even Prime Number"
Elseif a = 3 Then
Document.write "The Number is the Least Odd Prime Number"
Else
Document.write "The Number is NOT 0,1,2 or 3"
End If
ElseIf a < 0 Then
Document.write "The Number is a NEGATIVE Number"
Else
Document.write "The Number is ZERO"
End If </script> </body> </html>

switch statement
Select Case expression
Case expressionlist1
statement …
Case expressionlist2
Statement…
Case expressionlistn
Statement…
Case Else
elsestatement
Elsestatement
End Select

<html> <body>
<script language="vbscript" type="text/vbscript">
Dim MyVar : MyVar = 1
Select case MyVar
case 1
Document.write "The Number is the Least Composite
Number"
case 2
Document.write "The Number is the only Even Prime
Number"
case 3
Document.write "The Number is the Least Odd Prime
Number"
case else
Document.write "Unknown Number"
End select
</script> </body> </html>

Loops
•allows us to execute a statement or group of
statements multiple times

Loops
•for loop
•for ..each loop
•while..wend loop
•do..while loops
•do..until loops

for loop
•write a loop that needs to execute a specific number of
times.
For counter = start To end [Step stepcount]
[statement 1]
...
[statement n]
Next

<html>
<body>
<script type="text/vbscript">
Dim a : a=10
For i=0 to a Step 2
document.write("The value of i is : " & i)
document.write("<br></br>")
Next
</script>
</body>
</html>

For…Each loop
For Each element In Group
[statement 1]
[statement 2]
....
[statement n]
Next

<html>
<body>
<script language="vbscript" type="text/vbscript">
fruits=Array("apple","orange","cherries")
Dim fruitnames
For each item in fruits
fruitnames=fruitnames&item
Next
msgbox fruitnames
</script>
</body>
</html>

While..Wend loop
While condition(s)
[statements 1]
[statements 2]
...
[statements n]
Wend

<html>
<body>
<script language="vbscript" type="text/vbscript">
Dim Counter : Counter = 10
While Counter < 15 ' Test value of Counter.
Counter = Counter + 1 ' Increment Counter.
document.write("The Current Value of the Counter is : "
& Counter)
document.write("<br></br>")
Wend ' While loop exits if Counter Value becomes 15.
</script>
</body>
</html>

Do..while loops
Do While condition
[statement 1]
[statement 2]
...
[statement n]
[Exit Do]
[statement 1]
[statement 2]
...
[statement n]
Loop

<html>
<body>
<script language="vbscript" type="text/vbscript">
Do While i < 5
i = i + 1
Document.write("The value of i is : " & i)
Document.write("<br></br>")
Loop
</script>
</body>
</html>

Do..while loop alternate syntax
Do
[statement 1]
[statement 2]
...
[statement n]
[Exit Do]
[statement 1]
[statement 2]
...
[statement n]
Loop While condition

<html>
<body>
<script language="vbscript" type="text/vbscript">
i=10
Do
i = i -1
Document.write("The value of i is : " & i)
Document.write("<br></br>")
Loop While i<3 'Condition is false.Hence loop is executed once.
</script>
</body>
</html>

Do..Until loop
Do Until condition
[statement 1]
[statement 2]
..
[statement n]
[Exit Do]
[statement 1]
[statement 2]
...
[statement n]
Loop

<html>
<body>
<script language="vbscript" type="text/vbscript">
i=10
Do Until i>15 'Condition is False.Hence loop will be
executed
i = i + 1
Document.write("The value of i is : " & i)
Document.write("<br></br>")
Loop
</script>
</body>
</html>

Alternate syntax
Do
[statement 1]
[statement 2]
...
[statement n]
[Exit Do]
[statement 1]
[statement 2]
...
[statement n]
Loop Until condition

<html>
<body>
<script language="vbscript" type="text/vbscript">
i=10
Do
i = i + 1
Document.write("The value of i is : " & i)
Document.write("<br></br>")
Loop Until i<15 'Condition is True.Hence loop is executed
once.
</script>
</body>
</html>

Loop Control Statements:

Exit For Statement

<html>
<body>
<script language="vbscript" type="text/vbscript">
Dim a : a=10
For i=0 to a Step 2 'i is the counter variable and it is incremented
by 2
document.write("The value is i is : " & i)
document.write("<br></br>")
If i=4 Then
i=i*10 'This is executed only if i=4
document.write("The value is i is : " & i)
Exit For 'Exited when i=4
End If
Next
</script>
</body>
</html>

Exit Do Statement

<html>
<body>
<script language="vbscript" type="text/vbscript">
i = 0
Do While i <= 100
If i > 10 Then
Exit Do ' Loop Exits if i>10
End If
document.write("The Value of i is : " &i)
document.write("<br></br>")
i = i + 2
Loop
</script>
</body>
</html>

Array
1.Using Dim
Dim arr1() 'Without Size
2. Mentioning the Size
Dim arr2(5)'Declared with size of 5
Store 6 elements at 0 to 5 index
3. using 'Array' Parameter
Dim arr3
arr3 = Array("apple","Orange","Grapes")

•Store values from 0 to size index
•Array index can never be –ve
•can store an integer, string or characters in a
single array variable.

Assigning Values to an Array
•by specifying array index value against each
one of the values to be assigned.
Dim arr(5)
arr(0) = "1" 'Number as String
arr(1) = "VBScript“ 'String
arr(2) = 100 'Number
arr(3) = 2.45 'Decimal Number
arr(4) = #10/07/2013#'Date
arr(5) = #12.45 PM# 'Time

Multi Dimension Arrays
Dim arr(2,3)
arr(0,0) = "Apple"
arr(0,1) = "Orange"
arr(0,2) = "Grapes"
arr(0,3) = "pineapple"
arr(1,0) = "cucumber"
arr(1,1) = "beans"
arr(1,2) = "carrot"
arr(1,3) = "tomato"
arr(2,0) = "potato"
arr(2,1) = "sandwitch"
arr(2,2) = "coffee“
arr(2,3) = "nuts"

Redim Statement
•ReDimStatement is used to Declare dynamic-
array variables and allocate or reallocate storage
space.
ReDim[Preserve] varname(subscripts)
•Preserve -An Optional parameter used to
preserve the data in an existing array
•varname-A Required parameter, which denotes
Name of the variable
•subscripts -A Required parameter, which
indicates the size of the array.

Dim a()
i=0
redima(5)
a(0)="XYZ"
a(1)=41.25
a(2)=22
REDIM PRESERVE a(7)
For i=3 to 7
a(i)= i
Next 'to Fetch the output
For i=0 to ubound(a)
Msgboxa(i)
Next

various inbuilt functions
Lbound
•The LBound Function returns the smallest
subscript of the specified array
LBound(ArrayName[,dimension]).

Dim arr(5)
arr(0) = "1" 'Number as String
arr(1) = "VBScript 'String
arr(2) = 100 'Number
arr(3) = 2.45 'Decimal Number
arr(4) = #10/07/2013# 'Date
arr(5) = #12.45 PM# 'Time
document.write("The smallest Subscript value of the given
array is : " & LBound(arr))
' For MultiDimensionArrays :
Dim arr2(3,2)
document.write("The smallest Subscript of the firstdimension
of arr2 is : " & LBound(arr2,1) & "<br/>")
document.write("The smallest Subscript of the Second
dimension of arr2 is : " & LBound(arr2,2) & "<br/>")

UBound
•UBound Function returns the Largest subscript
of the specified array
•UBound(ArrayName[,dimension])

Dim arr(5)
arr(0) = "1" 'Number as String
arr(1) = "VBScript" 'String
arr(2) = 100 'Number
arr(3) = 2.45 'Decimal Number
arr(4) = #10/07/2013# 'Date
arr(5) = #12.45 PM# 'Time
document.write("The Largest Subscript value of the given
array is : " & UBound(arr))
' For MultiDimensionArrays :
Dim arr2(3,2)
document.write("The Largest Subscript of the first
dimension of arr2 is : " & UBound(arr2,1) & "<br/>")
document.write("The Largest Subscript of the Second
dimension of arr2 is : " & UBound(arr2,2) & "<br/>")

Split
•A Split Function returns an array that contains a
specific number of values splittedbased on a Delimiter.
Split(expression[,delimiter[,count[,compare]]])
a=Split("Red $ Blue $ Yellow","$")
b=ubound(a)
For i=0 to b
document.write("The value of array in " & i& " is :" & a(i)&
"<br/>")
Next

Join
•A Function, which returns a String that contains a specified
number of substrings in an array
Join(List[,delimiter])
' Join using spaces
a = array("Red","Blue","Yellow")
b = join(a)
document.write("The value of b " & " is :" & b & "<br/>")
' Join using $
b = join(a,"$")
document.write("The Join result after using delimiter is : "
& b & "<br/>")

Filter
•A Filter Function, which returns a zero-based
array that contains a subset of a string array
based on a specific filter criteria.
Filter(inputstrings,value[,include[,compare]])
Filter(str,”B”,0,0)
Filter(str,”B”,1,0)
Filter(str,”B”,0,1)

a= array("Red","Blue","Yellow")
b = Filter(a,"B")
c = Filter(a,"e")
d = Filter(a,"Y")
For each x in b
Document.write("The Filter result 1: " & x & "<br/>")
Next
For each y in c
Document.write("The Filter result 2: " & y & "<br/>")
Next
For each z in d
Document.write("The Filter result 3: " & z & "<br/>")
Next

IsArray
•The IsArrayFunction returns a booleanvalue that
indicates whether or NOT the specified input
variable is an array variable.
IsArray(variablename)
a = array("Red","Blue","Yellow")
b = "12345"
Document.write("The IsArrayresult 1 : " &
IsArray(a) & "<br/>")
Document.write("The IsArrayresult 2 : " &
IsArray(b) & "<br/>")

Erase
•The Erase Function is used to reset the values
of fixed size arrays and free the memory of the
dynamic arrays. It behaves depending upon
the type of the arrays.
Erase ArrayName

Dim NumArray(3)
NumArray(0) = "VBScript"
NumArray(1) = 1.05
NumArray(2) = 25
NumArray(3) = #23/04/2013#
Dim DynamicArray()
ReDim DynamicArray(9) ' Allocate storage space.
Erase NumArray' Each element is reinitialized.
Erase DynamicArray' Free memory used by array.
' All values would be erased.
Document.write("The value at Zerothindex of NumArrayis " &
NumArray(0) & "<br/>")
Document.write("The value at First index of NumArrayis " &
NumArray(1) & "<br/>")
Document.write("The value at Second index of NumArrayis " &
NumArray(2) & "<br/>")
Document.write("The value at Third index of NumArrayis " &
NumArray(3) & "<br/>")

Dialog Box
•Alert Function-to display message to user
Alert(“messagestring")
•MsgBoxFunction-to display the message to
user
MsgBox(“messageString”,Buttontype,”title”)
•InputBox-to take the input from user
InputBox(“messageString”,”title”,”default-
text”,xpos,ypos)

Buttons parameter
Buttons to be displayed:
•0 --Displays OK button only. (Default)
•1 -Displays OK and Cancel buttons.
•2 Displays Abort, Retry, and Ignore buttons.
•3 Displays Yes, No, and Cancel buttons.
•4 Displays Yes and No buttons.
•5 Displays Retry and Cancel buttons.
Style of the box to be displayed
•16 Displays Critical Message icon.
•32 Displays Warning Query icon.
•48 Displays Warning Message icon.
•64 Displays Information Message icon.

RETURN VALUES from msgBox :
1 -OK was clicked
2 -Cancel was clicked
3 -Abort was clicked
4 -Retry was clicked
5 -Ignore was clicked
6 -Yes was clicked
7 -No was clicked

<html>
<body>
<script type="text/vbscript">
MsgBox("Welcome")
a = MsgBox(“Do you want to enter
number?",3,”title_msg")
document.write("The Value of a is " & a)
</script>
</body>
</html>

•Output:
•The Value of a is 6
•The Value of a is 7
•The Value of a is 2

<html>
<body>
<script type="text/vbscript">
InputBox("Enter a number")
a=InputBox("Enter a Number","EnterValue")
msgboxa
b=InputBox("Enter a Number","EnterValue",123)
msgboxb
c=InputBox("Enter your name","EnterValue",123,700)
msgboxc
d=InputBox("Enter your name","EnterValue",123,,500)
Msgboxd
</script>
</body>
</html>

NUMBERS
•Number functions help us to convert a given
number from one data subtype to another
data subtype.
•syntax
variable = function_name(expression)

Procedures/ Function
1.Sub Procedure
EgMsgBox(“Hello”)
1.Function Procedure
eg. lntSum=AddNumbers(10, 20)

Sub Procedures
•Similar to functions but does not return a
value while function may or may not return a
value.
Sub Procedure_name()
Statement…
………………….
End Sub
Procedure_name
Procedure_name()
Call Procedure_name()

<html>
<body>
<script type="text/vbscript">
Sub sayHello()
msgbox("Hello world")
End Sub
sayHello()
</script>
</body>
</html>

output

<html>
<body>
<script type="text/vbscript">
Sub printval(val)
Msgbox(val)
End Sub
printval(5)/ call printval(5)
</script>
</body>
</html>

•Cannot invoke subprocedure in an expression
<script type="text/vbscript">
sub square()
Dim x
x=10
End sub
document.write(x &"<br>"&square())
</script>

<html>
<body>
<script type="text/vbscript">
sub method1()
document.write("hello")
method2()
End sub
sub method2()
document.write("world")
End sub
method1()
</script>
</body>
</html>

<html>
<body>
<script type="text/vbscript">
Sub printval(val1,val2)
Msgbox(val1 & val2)
End Sub
Printval 5,10 / call printval(5,10)
</script>
</body>
</html>

Function
•is a group of reusable code which can be
called anywhere in your program
•Syntax
Function Function_name()
Statement….
……………….…
End Function
Call Function_name()
X= function_name()
function_name val1, val2

<html>
<body>
<script type="text/vbscript">
Function sayHello()
msgbox("Hello there")
End Function
</script>
Call sayHello()
</body>
</html>

Function Parameters
<html>
<body>
<script language="vbscript" type="text/vbscript">
Function sayHello(name, age)
msgbox( name & " is " & age & " years old.")
End Function
Call sayHello(“ABC", 20)
</script>
</body>
</html>

returning a value
<html>
<body>
<script type="text/vbscript">
Function square(no)
Square= no*no
End function
Dim temp
temp=square(5)
MsgBoxtemp
</script>
</body> output: 25
</html>

<html>
<body>
<script type="text/vbscript">
Dim temp
for i=0 to 5
temp=square(i)
document.write(temp &"<br>")
next
Function square(no)
if no=3 then
exit function
end if
Square= no*no
End function
</script>
</body>
</html>

output:

•A function can return multiple values separated by comma
<html>
<body>
<script type="text/vbscript">
Function concatenate(first, last)
Dim full
full = first & last
concatenate = full
End Function
dim result
result = concatenate(“123", “abc")
msgbox(result)
</script>
</body>
</html>

<html>
<body>
<script type="text/vbscript">
Function retval(val)
retval= val+val
document.write(val*val& "<br>")
document.write("hello" & "<br>")
End Function
dim result
result = retval(3)
document.write(result & "<br>")
</script>
</body>
</html>

•output: 9
hello
6

<html>
<body>
<script type="text/vbscript">
Function add(input1,input2)
add = input1 * input1
add = input2 * input2
End Function
dim result
result = add(2,3)
document.write(result)
</script> output: 9
</body>
</html>

<script type="text/vbscript">
function add(input1,input2)
Dim temp1,temp2,temp
temp1= input1*input1
temp2= input2*input2
temp = temp1 & "*" & temp2
add = temp
end function
dim result
result = add(2,3)
a=Split(result,"*")
b=ubound(a)
For i=0 to b
document.write(a(i)& "<br/>")
Next
</script>

<html>
<body>
<script type="text/vbscript">
Function add(input1,input2,output1,output2)
output1 = input1 * input1
output2 = input2 * input2
End Function
call add(2,3,x,y)
document.write(x & "<br>")
document.write(y)
</script>
</body>
</html>

Call by value
<html>
<body>
<script type="text/vbscript">
Function fnadd(Byval num1, Byval num2)
num1 = 4
num2 = 5
End Function
Dim x,y
x=6
y=4
res= fnadd(x,y)
document.write("The value of x is " & x & "<br />")
document.write("The value of y is " & y & "<br />")
</script>
</body>
</html>

•Output : The value of x is 6
•The value of y is 4

Call by reference
<html>
<body>
<script language="vbscript" type="text/vbscript">
Function fnadd(ByRefnum1, ByRefnum2)
num1 = 4
num2 = 5
End Function
Dim x,y
x=6
y=4
res= fnadd(x,y)
document.write("The value of x is " & x & "<br/>")
document.write("The value of y is " & y & "<br/>")
</script>
</body>
</html>

•Output : The value of x is 4
•The value of y is 5

<html>
<body>
<script type="text/vbscript">
Dim retValue
Call ShowSum(3, 4)
retValue= ShowSum(7, 8)
MsgBox"The function returned: " & retValue
Function ShowSum(value1, value2)
Dim sum
sum = value1 + value2
MsgBox"The sum is: " & sum
ShowSum= sum
End Function
</script>
</body>
</html>

output: 7
15
15

•By default cal byref
Eg
Sub retval(val)
val=10
End Sub
X=50
Retval x
Document.write(x)

output: 10
Byval : 50
Byref : 10

•No effect on byref
Eg
Sub retval(val)
val=10
End Sub
X=50
Retval(x)
Document.write(x)
Output : byval: 50
byref: 50

•Remain same
Eg
Sub retval(val)
val=10
End Sub
X=50
Call Retval(x)
Document.write(x)
Output : byval: 50
byref: 10

•No effect on byref
Eg
Sub retval(val)
val=10
End Sub
X=50
Call Retval((x))
Document.write(x)
Output : byval: 50
byref: 50

If returns value
•Eg
Function f1(val)
Val = val+1
F1=val
End function
X= 50
Temp= f1 x
Document.write(x)
Document.write(temp)
error

•Eg
Function f1(val)
Val = val+1
F1=val
End function
X= 50
Temp=call f1( x)
Document.write(x)
Document.write(temp)
error

•By default byref
•Eg
Function f1(val)
Val = val+1
F1=val
End function
X= 50
Temp=f1( x)
Document.write(x)
Document.write(temp)
Output: 51
byval50
byref51

Dim arr(2,2)
arr(0,0) = "Apple"
arr(0,1) = "Orange"
arr(0,2) = "Grapes"
arr(1,0) = "cucumber"
arr(1,1) = "beans"
arr(1,2) = "carrot"
arr(2,0) = "potato"
arr(2,1) = "sandwitch"
arr(2,2) = "coffee"
call display(arr)
Function display(array())
for i= 0 to 2
for j =0 to 2
document.write(array(i,j) & " ")
next
document.write("<br>")
next
end function

Strings
•Strings are a sequence of characters, which
can consist of alphabets or numbers or special
characters or all of them.
•A variable is said to be a string if it is enclosed
within double quotes " ".
•Ex- str1= “12345”
Str2= “Hello World”

String functions
•InStr, InstrRev
•Lcase, Ucase
•Left,Right
•Mid
•Ltrim,Rtrim
•Len
•Replace
•Space
•StrComp
•StrReverse

InStr Function
•The InStr Function returns the first occurrence
of one string within another string.
•The search happens from left to right.
•Syntax
InStr([start,]string1,string2[,compare])

<html>
<body>
<script language="vbscript" type="text/vbscript">
var="GLA University"
document.write("Line 1 : " & InStr(1,var,"U") & "<br/>")
document.write("Line 2 : " & InStr(7,var,"s") & "<br/>")
document.write("Line 3 : " & InStr(1,var,"r",1) & "<br/>")
document.write("Line 4 : " & InStr(1,var,"t",0) & "<br/>")
document.write("Line 5 : " & InStr(1,var,"i") & "<br/>")
document.write("Line 6 : " & InStr(8,var,"i") & "<br/>")
document.write("Line 7 : " & InStr(var,"UNi"))
</script>
</body>
</html>

InStrRev Function
•The InStrRev Function returns the first
occurrence of one string within another string.
The Search happens from right to Left.
•Syntax
InStrRev(string1,string2[,start,[compare]])

<html>
<body>
<script language="vbscript" type="text/vbscript">
var="GLA University"
document.write("Line 1 : " & InStrRev(var,"U",1) & "<br/>")
document.write("Line 2 : " & InStrRev(var,"s",12) & "<br/>")
document.write("Line 3 : " & InStrRev(var,"r",12,1) & "<br/>")
document.write("Line 4 : " & InStrRev(var,"t",10,0) & "<br/>")
document.write("Line 5 : " & InStrRev(var,"i",1) & "<br/>")
document.write("Line 6 : " & InStrRev(var,"i",8) & "<br/>")
document.write("Line 7 : " & InStrRev(var,"UNi"))
</script>
</body>
</html>

Lcase , Ucase Function
•The LCaseFunction returns the string after
converting the entered string into lower case
letters.
•Syntax
Lcase(String)
•The UCaseFunction returns the string after
converting the entered string into UPPER case
letters.
•Syntax
UCase(String)

Left, Right Function
•The Left Function returns a specified number of
characters from the left side of the given input
string.
•Syntax
Left(String, Length)
•The Right Function returns a specified number of
characters from the right side of the given input
string.
•Syntax
Right(String, Length)

Mid, Ltrim, Rtrim,Trim Function
•The Mid Function returns a specified number of characters
from a given input string.
•Syntax
Mid(String,start[,Length])
•The LtrimFunction removes the blank spaces that are there
on the left side of the string.
•Syntax LTrim(String)
•The RtrimFunction removes the blank spaces that are
there on the Right side of the string.
•Syntax RTrim(String)
•The Trim Function removes both the Leading and Trailing
blank spaces of the given input string.
•Syntax Trim(String)

<html>
<body>
<script language="vbscript" type="text/vbscript">
var="GLA University"
var1= " Mathura "
document.write("Line 1 : " & Lcase(var) & "<br/>")
document.write("Line 2 : " & Ucase(var) & "<br/>")
document.write("Line 3 : " & Left(var,3) & "<br/>")
document.write("Line 4 : " & Left(var,7) & "<br/>")
document.write("Line 5 : " & Right(var,3) & "<br/>")
document.write("Line 6 : " & Right(var,7) & "<br/>")
document.write("Line 7 : " & Mid(var,3) & "<br/>")
document.write("Line 8 : " & Mid(var,7,2) & "<br/>")
document.write("Line 9 : " & ltrim(var1) & "<br/>")
document.write("Line 10 : " & rtrim(var1) & "<br/>")
document.write("Line 11 : " & Trim(var1) & "<br/>")
</script>
</body>
</html>

Len , Replace Function
•The Len Function returns the length of the given
input string including the blank spaces.
•Syntax Len(String)
•The Replace Function replaces a specified part of
a string with a specific string a specified
•number of times.
•Syntax
Replace(string,find,replacewith[,start[,count[,co
mpare]]])

<html>
<body>
<script language="vbscript" type="text/vbscript">
var="GLA University"
var1= " Mathura "
document.write("Line 1 : " & Len(var) & "<br/>")
document.write("Line 2 : " & Len(var1) & "<br/>")
document.write("Line 3 : " & Replace(var,"A","M") & "<br/>")
document.write("Line 4 : " & Replace(var,"ver","Uni") & "<br/>")
document.write("Line 5 : " & Replace(var,"ver","Uni",3) & "<br/>")
document.write("Line 6 : " & Replace(var,"ver","Uni",8) & "<br/>")
document.write("Line 7 : " & Replace(var,"ver","Uni",11) & "<br/>")
document.write("Line 8 : " & Replace(var,"i","Uni",3,2) & "<br/>")
document.write("Line 9 : " & Replace(var,"r","Uni",8,2) & "<br/>")
document.write("Line 10 : " & Replace(var,"ver","Uni",5,2) & "<br/>")
</script>
</body>
</html>

Space Function
•The Space Function fills a string with a specific
number of spaces.
•Syntax space(number)
•var1=“GLA"
•var2=“University"
•document.write(var1 & Space(2)& var2)
Output : GLA University

StrComp Function
•The StrCompFunction returns an integer value after
comparing the two given strings.
•If String 1 < String 2 then StrCompreturns -1
•If String 1 = String 2 then StrCompreturns 0
•If String 1 > String 2 then StrCompreturns 1
•Syntax
StrComp(string1,string2[,compare])
•var1=“GLA"
•var2=“University“
•Var3=“ GLA ”
document.write(strcomp(var1,var2) & "<br/>")
document.write(strcomp(var2,var1) & "<br/>")
document.write(strcomp(var1,var3) & "<br/>")
OutPut:

String Function
•The String Function fills a string with the specified character
the specified number of times.
•Syntax
•String(number,character)
document.write("Line 1 :" & String(3,"$") & "<br/>")
document.write("Line 2 :" & String(4,"*") & "<br/>")
document.write("Line 3 :" & String(5,100) & "<br/>")
document.write("Line 4 :" & String(6,"ABCDE") )
document.write("Line 5 :" & String(5,64) )
Output :
•Line 1 :$$$
•Line 2 :****
•Line 3 :ddddd
•Line 4 :AAAAAA
•Line 5 : @@@@@

StrReverse Function
•The StrReverse Function reverses the specified
string.
•Syntax StrReverse(string)
•document.write("Line 1 : " & StrReverse(“GLA") & "<br />")
•document.write("Line 2 : " & StrReverse(“24.6") & "<br />")
Output :
•Line 1 : ALG
•Line 2 : 6.42

Date and Time Functions
•The Function returns the current system Date.
•Syntax date()
•Ex a = date()
document.write("The Value of a : " & a)
Output:
The Value of a : 10/25/2016
(m/d/yyyy)

CDateFunction
•The Function converts a valid date and time expression
to type date.
•Syntax cdate(date)
ex: a = cdate("Jan 01 2020")
document.write("The Value of a : " & a)
document.write("<br/>")
b = cdate("31 Dec 2050")
document.write("The Value of b : " & b)
•Output:
The Value of a : 1/1/2020
The Value of b : 12/31/2050

DateAddFunction
•A Function, which returns a date to which a
specified time interval has been added.
•Syntax DateAdd(interval,number,date)
•Interval: d, m, yyyy, ww, q , h , n , s
•Number : +ve,-ve

date1=date()
document.write("Line 0 : " & date1 & "<br />")
document.write("Line 1 : " &DateAdd("yyyy",2,date1) & "<br />")
document.write("Line 2 : " &DateAdd("q",1,date1) & "<br />")
document.write("Line 3 : " &DateAdd("m",1,date1) & "<br />")
document.write("Line 4 : " &DateAdd("d",1,date1) & "<br />")
document.write("Line 5 : " &DateAdd("ww",1,date1) & "<br />")
document.write("Line 6 : " &DateAdd("h",1,"01-Jan-2016 12:00:00") & "<br />")
document.write("Line 7 : " &DateAdd("n",1,"01-Jan-2016 12:00:00") & "<br />")
Document.write("Line 8 : "&DateAdd("s",1,"01-Jan-2016 12:00:00") & "<br />")
Output:
Line 0 : 10/25/2016
Line 1 : 10/25/2018
Line 2 : 1/25/2017
Line 3 : 11/25/2016
Line 4 : 10/26/2016
Line 5 : 11/1/2016
Line 6 : 1/1/2016 1:00:00 PM
Line 7 : 1/1/2016 12:01:00 PM
Line 8 : 1/1/2016 12:00:01 PM

DateDiffFunction
•It is a function that returns the difference
between two specified time intervals.
•Syntax DateDiff(interval, date1, date2)

fromDate="01-Jan-09 00:00:00"
toDate="01-Jan-10 23:59:00"
document.write("Line 1 : “ &DateDiff("yyyy",fromDate,toDate))
document.write("Line 2 : " &DateDiff("q",fromDate,toDate))
document.write("Line 3 : " &DateDiff("m",fromDate,toDate))
document.write("Line 4 : " &DateDiff("y",fromDate,toDate))
document.write("Line 5 : " &DateDiff("d",fromDate,toDate))
document.write("Line 6 : " &DateDiff("ww",fromDate,toDate))
document.write("Line 7 : " &DateDiff("h",fromDate,toDate))
document.write("Line 8 : " &DateDiff("n",fromDate,toDate))
document.write("Line 9 : "&DateDiff("s",fromDate,toDate))
Output: Line 1 : 1
Line 2 : 4
Line 3 : 12
Line 4 : 365
Line 5 : 365
Line 6 : 52
Line 7 : 8783
Line 8 : 527039
Line 9 : 31622340

DatePartFunction
•It is a function that returns the specific part of
a given date.
•Syntax DatePart(interval,date)
•Ex Date1 = "2013-01-15“
a= DatePart("q", Date1)
b= DatePart(“d", Date1)
c= DatePart(“ww", Date1)
d= DatePart(“m", Date1)

DateSerial Function
•It is a function that returns a date for the
specified day, month and year parameters.
•Syntax DateSerial(year,month,day)
•Ex document.write(DateSerial(2016,10,25))
•Output : 10/25/2016

FormatDateTimeFunction
•It is a function that helps the developers to format and
return a valid date and time expression.
•Syntax FormatDateTime(date,format)
d=("2016-10-25 20:25")
document.write("Line 1 : " & FormatDateTime(d,0) )
document.write("Line 2 : " & FormatDateTime(d,1))
document.write("Line 3 : " & FormatDateTime(d,2) )
document.write("Line 4 : " & FormatDateTime(d,3) )
document.write("Line 5 : " & FormatDateTime(d,4))
Output: Line 1 : 10/25/2016 8:25:00 PM
Line 2 : Tuesday, October 25, 2016
Line 3 : 10/25/2016
Line 4 : 8:25:00 PM
Line 5 : 20:25

IsDateFunction
•It is a function that returns a Boolean Value
whether or Not the given input is a date.
•Syntax IsDate(expression)
•Ex returns True
document.write("Line 1 : " & IsDate("Nov 03, 1950") )
document.write("Line 2 : " & IsDate(#01/31/20#) )
document.write("Line 3 : " & IsDate(#05/31/20 10:30 PM#))

Day Function
•The Day function returns a number between 1 and 31 that
represents the day of the specified date.
•Syntax Day(date)
•The Month function returns a number between 1 and 12
that represents the month of the specified date.
•Syntax Month(date)
•The Year function returns an integer that represents a year
of the specified date.
•Syntax Year(date)
Ex date1= "2016-10-25"
document.write(day(date1))
document.write(month(date1))
document.write(Year(date1))

MonthName Function
•The MonthNamefunction returns the name of the month
for the specified date.
•Syntax MonthName(month[,toabbreviate])
MonthName(01,True) Jan
MonthName(01,false) January
•The WeekDayfunction returns an integer from 1 to 7 that
represents the day of the week for the specified date.
•Syntax Weekday(date)
•The WeekDayNamefunction returns the name of the
Weekday for the specified day.
•Syntax WeekdayName(weekday)

Time Functions
•The Function Now returns the current system date and time.
•Syntax Now()
exa = Now()
document.write("The Value of a : " & a)
Output: The Value of a : 10/25/2016 8:21:39 AM
•The Hour Function returns a number between 0 and 23 that
represents the hour of the day for the specified time stamp.
•Syntax Hour(time)
•The Minute Function returns a number between 0 and 59 that
represents the Minute of the hour for the specified time stamp.
•Syntax Minute(time)
•The Second Function returns a number between 0 and 59that
represents the Second of the hour for the specified time stamp.
•Syntax Second(time)

Time Function
•The Time Function returns the current system time.
•Syntax Time()
•Ex. b = Time()
document.write("The Value of b : " & b)
output: The Value of b : 8:34:46 AM
•The Timer Function returns the number of seconds and
milliseconds since 12:00 AM.
•Syntax Timer()
•The TimeSerialfunction returns the time for the specified hour,
minute and second values.
•Syntax TimeSerial(hour,minute,second)
•The TimeValueFunction converts the given input string to a valid
time.
•Syntax TimeValue(StringTime)

Events
•events that occur when the user or browser
interacts a page.
•Ex
•When the page loads
•When the user clicks a button
•pressing any key
•Closing ,resizing window

onclick Event
•occurs when a user clicks mouse's left button.
<html>
<head>
<script language="vbscript" type="text/vbscript">
Function sayHello()
msgbox "Hello World"
End Function
</script>
</head>
<body>
<input type="button" onclick="sayHello()" value="Say Hello"/>
</body>
</html>

onsubmit Event
•occurs when you try to submit a form
•Used for form validation
<html>
<head></head>
<body>
<script language="VBScript">
Function fnSubmit()
Msgbox(“GLA University")
End Function
</script>
<form action="/cgi-bin/test.cgi" method="post" name="form1"
onSubmit="fnSubmit()">
<input name="txt1" type="text"><br>
<input name="btnButton1" type="submit" value="Submit">
</form>
</script>
</body>
</html>

onmouseoverand onmouseout
•The onmouseover event occurs when you
bring your mouse over any element
•The onmouseout occurs when you take your
mouse out from that element.
<input name="txt1" type="text"
onMouseOut="AlertMsg()"><br>
<input name="txt2" type="text“
onMouseOver="onmouse_over() “><br>
<input name="btnButton" type="button" value="Submit">

<script language="VBScript">
Function AlertMsg
Msgbox("ALERT !")
End Function
Function onmourse_over()
Msgbox("Onmouse Over")
End Function
Sub txt2_OnMouseOut()
Msgbox("Onmouse Out !!!")
End Sub
Sub btnButton_OnMouseOut()
Msgbox("onmouse out on Button !")
End Sub
</script>

Other events
•Onchange-when the element changes
•Onsubmit-when the form is submitted
•Onreset-when the form is reset
•Onblur-when the element loses focus
•Onfocus-when the element gets focus
•Onkeydown-when key is pressed
•Onkeypress-when key is pressed and released
•Onkeyup-when key is released
•Onclick-when a mouse click
•Ondblclick-when a mouse double-click

•Onmousedown -when mouse button is
pressed
•Onmouseout -when mouse pointer moves
out of an element
•Onmouseover -when mouse pointer moves
over an element
•Onmouseup -when mouse button is released
•Onmousemove -when mouse pointer moves

<html>
<head>
<script type=text/vbscript>
sub strt()
document.bgcolor="red"
end sub
</script>
</head>
<body>
<form>
<center><input type="button" onclick="strt()"
value="change color"></center>
</form>
</body>
</html>

<html>
<head>
<script type=text/vbscript>
Sub b1_onclick()
t2.style.backgroundColor="red"
end sub
sub b2_onclick()
document.bgcolor="red"
end sub
</script>
</head>
<body>
<p><center ><h1 id="t2">hello world</h1></center></p>
<form>
<center><input type="button" value="start" id= "b1"/><br></center>
<center><input type="button" name = "b2" value="change color"></center>
</form>
</body>
</html>

<html>
<HEAD>
<SCRIPT type ="text/VBScript">
dim W
function fnOpen()
set W = window.open()
W.document.write("hello")
W.document.bgcolor="red"
end function
function fnClose()
W.close()
end function
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<p>Hello World</p>
<INPUT TYPE="button" ID="btnOpen" onclick="fnOpen()" VALUE="Open New Window">
<INPUT TYPE="button" ID="btnClose" onclick="fnClose()" VALUE="CloseWindow">
</FORM>
</body>
</html>

validation
<html>
<head>
<script type="text/vbscript">
function check()
n=t1.value
dim l
l=len(n)
if l > 6 then
MsgBox"nameis valid"
else
MsgBox"PleaseEnter more than 6 character"
end if
end function
</script>
<body>
<center>name:<input type="text" name="t1">
<br>
<input type="button" value="check" onclick="check()">
</center>
</body>
</html>

<html>
<head>
<script type="text/vbscript">
function check()
n=p1.value
dim l,m
l=Instr(1,n,"@")
document.write(l)
if l = 0 then
MsgBox"emaildoesnotcontain @"
else
MsgBox"emailcontain @"
end if
end function
</script>
<body>
Email id :<input type="text" name="p1">
<br>
<input type="button" value="check" onclick="check()">
</body>
</html>
Tags