VBScript Language
VBScript Language
for
for
IV Year IT Students
IV Year IT Students
2 2
E-Commerce
E-Commerce
Electronic Commerce
Electronic Commerce -- the
the use o
use of Web
f Web sit
sites t
es to
o
sell goods and services over the Internet.
sell goods and services over the Internet.
The World Wide Web (Web)
The World Wide Web (Web) -- a b
a bod
ody of
y of sof
softw
tware
are
and a set of protocols and
and a set of protocols and conventions based on
conventions based on
hypertext and multimedia that make the Internet
hypertext and multimedia that make the Internet
easy to use and browse.
easy to use and browse.
hypertext
hypertext -- a ca
a capabi
pability to
lity to jump
jump betw
between an
een and
d
within documents virtually at will.
within documents virtually at will.
client/serve
client/server
r computing
computing -- pro
proces
cessin
sing is s
g is sha
hared
red
between multiple small computers known as
between multiple small computers known as
clients
clients that are connected via a network to a
that are connected via a network to a host
host
computer known as a server.
computer known as a server.
uniform resource locator (URL)
uniform resource locator (URL) ±
± pr
prot
otoc
ocol t
ol tha
hatt
allows your browser to communicate with remote
allows your browser to communicate with remote
hosts.
2 2
E-Commerce
E-Commerce
Electronic Commerce
Electronic Commerce -- the
the use o
use of Web
f Web sit
sites t
es to
o
sell goods and services over the Internet.
sell goods and services over the Internet.
The World Wide Web (Web)
The World Wide Web (Web) -- a b
a bod
ody of
y of sof
softw
tware
are
and a set of protocols and
and a set of protocols and conventions based on
conventions based on
hypertext and multimedia that make the Internet
hypertext and multimedia that make the Internet
easy to use and browse.
easy to use and browse.
hypertext
hypertext -- a ca
a capabi
pability to
lity to jump
jump betw
between an
een and
d
within documents virtually at will.
within documents virtually at will.
client/serve
client/server
r computing
computing -- pro
proces
cessin
sing is s
g is sha
hared
red
between multiple small computers known as
between multiple small computers known as
clients
clients that are connected via a network to a
that are connected via a network to a host
host
computer known as a server.
computer known as a server.
uniform resource locator (URL)
uniform resource locator (URL) ±
± pr
prot
otoc
ocol t
ol tha
hatt
allows your browser to communicate with remote
allows your browser to communicate with remote
hosts.
3 3
Displaying Information with a
Displaying Information with a
Browser
Browser
When
When dispdisplaylaying ing infoinformatrmation ion sensent to t to it frit from om the the serserverver, th, thee browser processes formatting instructions included in the browser processes formatting instructions included in the text file retrieved from the
text file retrieved from the server.server. Th
The e seservrver er ststorores es ththe e dadata ta wiwithth tagstags to indicate how text andto indicate how text and other forms of information will be displayed.
other forms of information will be displayed. The
The tatags gs in tin the he WorlWorld d Wide Wide Web Web arare pae part rt of a of a spspececialial publishing language called
publishing language called hypertext markup languagehypertext markup language (HTML)
(HTML).. Do
Docucumementnts s on ton the he Web Web arare ree refeferrrred ed to ato ass Web pagesWeb pages andand their location is a
their location is a Web siteWeb site.. Sin
Since Hce HTML iTML is stas standndarard for ad for all coll compumputetersrs, an, any Web by Web brorowswser er can request an HTML docum
4
Commonly Used HTML tags
Tag Operation Example Result <B> Boldface text <B>Hello</B> Hello
<I> Italicized text <I>Hello</I> Hello
<CENTER> Centered text <CENTER>Hello</CEN TER>
Hello
<BR> Line Break End this line.<BR> Start another line.
End this line. Start another line. <HTML> Begin/End HTML <HTML>«</HTML> Begins and Ends
Web Page <TITLE> Begin/End Title of
Web Page
<TITLE>Web page for CH. 13</TITLE>
³Web page for CH. 13´ appears in header bar of browser.
5
Commonly Used HTML tags (Cont.)
Tag Operation Example Result <BODY> Begin/End Body
of Web page
<BODY>«</BODY> Begins and ends the body of the web page <P> Start/End paragraph <P>Paragraph</P><P>New Paragraph</P> Paragraph New Paragraph <H1> Create type 1 (largest) heading (also 2, 3, 4) <H1>Biggest</H1> <H2>Big</H2> <H4>Smallest</H4>
Biggest
Big Smallest <IMG SRC = «> Include image in web page<image src = ³family.jpg´> jpg image file named family is displayed <FORM> Create an input
form on a web page
<FORM NAME = Order>«</FORM>
Creates an input form named ³Order´
<INPUT TYPE = text>
Create text box for input
<INPUT TYPE = text NAME = txtOrder>
A text box for input is displayed
6
N
otes About HTML
HTML tags are enclosed in angle brackets
(< >) to set them off.
Most HTML tags work in pairs, but there
are some exceptions.
Script placed within the <HEAD>..</HEAD>
tags is not executed unless referred to in
the body of the HTML page.
Script placed in the body is automatically
executed as the Web page is loaded into
the browser.
7
G
eneral Form of Web Page
<HTML> <HEAD> <TITLE>
Title of Web page goes here </TITLE>
Other heading material goes here </HEAD>
<BODY>
Body of Web page goes here </BODY>
8
S
cripting in the Browser
An important use of Web browsers in electronic commerce is to enter information.
Validation of input and calculations on the browser can be carried out via a type of computer programming known as scripting.
Scripting is similar to other types of computer programming in that it uses variables and statements to carry out a desired set of logical operations, but it is also different.
Instead of being executed as a compiled program, it is
executed by another program, in our case the Web browser.
Scripting languages are easier to work with than compiled
languages.
A script takes longer to run than a compiled program since each instruction is being interpreted rather than being
9
S
cripting Languages
The two most widely used scripting languages for working with Web pages are Javascript and VBScript.
Javascript uses a C-like syntax and can run on either
Netscape or Internet Explorer browsers.
VBScript is based on Visual Basic, but runs only on Internet
Explorer.
Both Javascript and VBScript, when used on the browser, are termed client-side scripting since they are running on the Web client.
VBScript is also widely used in the Active Server Page
(ASP) approach to directing Microsoft Web server software
that runs on Windows NT or 2000 operating systems. This is called server-side scripting.
Wh
at is VBScript?
An interpreted programming language
primarily used to write relatively short programs
that access operating system and application
objects. Such languages are referred to as
"scripting" languages. The VBScript commands
are a subset of the more complete, compiled
Why
is VBScript used?
VBScript is an eas
y
to learn language and t
h
us is
a good candidate for a brief introduction to
programming.
VBScript, VB, and VBA (Visual Basic for
Applications) are collectivel
y
used more t
h
an an
y
ot
h
er language in t
h
e world so exposure to t
h
ese
languages is important.
VBScript can be used to automate tasks in web
pages, MS Excel spreads
h
eets, MS Access
Databases, and ot
h
er MS applications.
VBScript can be used to automate administrative
tasks suc
h
as moving or cop
y
ing files, creating
C
reating/Editing a VBScript file
VBScript programs are AS
C
II text files and can be edited
using an
y
text editor suc
h
as notepad.exe or pfe.exe.
Open
y
our ITE
C
110 student folder and create a folder
named lab
2
wit
h
in it.
As was discusses in lab 1 please verif
y
t
h
at t
h
e
tools->folder options->view tab->
h
ide file extensions for
known file t
y
pes is NOT c
h
ecked.
Open t
h
e lab
2
folder. Rig
h
t click an
y
w
h
ere in t
h
e
folder and c
h
oose "new text file" from t
h
e resulting
drop down menu. Rename t
h
e new text file to
Hello
W
orld.vbs .
Rig
h
t click on t
h
e new file, c
h
oose EDIT, and enter t
h
e
VBScript program on t
h
e next slide.
Wh
at is VBScript
Based on t
h
e Visual Basic famil
y
of languages
Supports object oriented features
Interpreted
Loosel
y
T
y
ped
Visual Basic & VBScript
Visual Basic
² Stand-alone (compiled
or interpreted)
² Variables are t
y
ped
VBScript
² Embedded in a
W
eb
page and executed b
y
browser
² Variables can be
associated wit
h
an
y
t
y
pe
² Uses most program
structures available in
VB, including loops
C
ontents
S yntax Rules Comments Naming Rules Data T ypes Storage² Variables, Constants, Arrays
Operators
² Arithmetic, Comparison, Logical, Assignment, Concatenation
Branc hing ² If ² Select Looping ² While ² Do«While ² For Sub/Functions ² Built-In ² User defined
S
y
ntax Rules
VBScript is not case sensitive
² Except for strings (denoted b
y
double-quotes)
Eac
h
statement can onl
y
exist on one line
² Unless
y
ou use a line continuation
Statements can be eit
h
er simple statements or
C
omments
R
em
commented statement
¶
commented statement
C
an be a complete line or a portion of a line.
VBScript does not support multi-line comments.
T
h
e comment s
y
mbol must be put at t
h
e start
Naming Rules
1. Must begin wit
h
a letter or t
h
e underscore
Good: count, strInfo, _data
Bad:
2da
y,
4get, *count violate t
his rule.
2
.
C
annot contain special c
h
aracters except t
h
e
underscore
Good: Names using alp
habets, numbers and t
he underscore.
Bad: Names using s
ymbols and ot
her special c
haracters.
3
.
C
annot matc
h
a reserved word
Bad: for, if, Select
4
. Must be unique wit
h
in a context
Naming
C
onvention
obj
² T
h
ree letter prefix for t
h
e data t
y
pe
DescriptiveName
² A descriptive name for t
h
e variable
² First letter of eac
h
word in upper case
Literal T
y
pes
Numeric
² 1
²
3.1
4²
3E
7Boolean
² True
² False
Ch
aracters
² ´Hello
Worldµ
² ´1
23Straw Laneµ
² ´
43210µ
Date
² #0
7/
21/
200
6#
² #Jan
6,
200
8#
Variables
Dim
variableName
Variables
h
old values t
h
at can c
h
ange during
program execution
T
h
e
y
function just like temporar
y
storage
locations
Declaration of variables is optional, but
recommended
C
onstants
C
onst
constant
Name
=value
C
onstant names
h
old values t
h
at
c
annot
c
h
ange
during program execution
C
onstants s
h
ould be used in place of
h
Operations
Operations are t
h
e man
y
forms of computations,
comparisons etc t
h
at can be performed in VB
Most operations are binar
y
in t
h
e form:
operand1 operator operand
2
Ot
h
er operations are unar
y
in t
h
e form
operator operand1
Wh
en an operator
h
as
2
s
y
mbols,
y
ou cannot separate
t
h
em wit
h
a space:
² Good:
2<>
3Arit
h
metic
Used to perform calculations
Bot
h
operands must be numeric values
T
h
e result is a numeric value
+
Addition
-
Subtraction, Negation
*
Multiplication
/
Division
\
Integer
Division
^
Exponent
Mod
Modulo
C
omparison
Used to compare t
h
e value of two items
Bot
h
operands must be of t
h
e same data t
y
pe
T
h
e result is a Boolean value
=
Equalit
y
<>
Inequalit
y
<
Less
t
h
an
>
Greater
t
h
an
<=
Less
t
h
an or equals
>=
Greater
t
h
an or equals
Logical
Logical
Used
Used to
to redu
reduce B
ce Bool
oolean
ean valu
values in
es into a
to a single
single Boo
Boolean
lean
value
value
B
Bo
ott
h
h
operands must be Boolean values
operands must be Boolean values
T
T
h
h
e result is a Boolean value
e result is a Boolean value
And
And
C
C
onjunction
onjunction
O
Orr
D
Diissjju
un
nccttiio
on
n
N
No
ott
N
Ne
egga
attiio
on
n
X
And Trut
And Trut
h
h
Table
Table
C
C
onjunction
onjunction
U
Usse
ed
d w
w
h
h
en bot
en bot
h
h
conditions are required
conditions are required
O
Op
pe
erra
an
nd
d1
1
O
Op
pe
erra
an
nd
d
2
2
Result
Result
T
T
T
T
T
T
T
T
F
F
F
F
F
F
T
T
F
F
F
F
F
F
F
F
Or Trut
Or Trut
h
h
Table
Table
D
Diissjju
un
nccttiio
on
n
U
Usse
ed
d w
w
h
h
en eit
en eit
h
h
er condition is sufficient
er condition is sufficient
O
Op
pe
erra
an
nd
d1
1
O
Op
pe
erra
an
nd
d
2
2
Result
Result
T
T
T
T
T
T
T
T
F
F
T
T
F
F
T
T
T
T
F
F
F
F
F
F
Not Trut
h
Table
Unar
y
Operand
Used to c
h
ange t
h
e value of a condition
Operand 1
Result
T
F
Xor Trut
h
Table
Exclusion
Used w
h
en eit
h
er condition s
h
ould be different
Operand1
Operand
2
Result
T
T
F
T
F
T
F
T
T
Assignment
Ch
anges t
h
e value of a variable
=
Uses t
h
e same s
y
mbol as an equalit
y
c
h
eck operator.
Assignment onl
y
occurs w
h
en used in an
y
of t
h
e
following forms:
variable = literal value
variable = expression
C
oncatenation
C
ombines
2
data t
y
pes for displa
y
as a string
Branc
h
ing
Allows
y
ou to avoid running certain sections of
y
our code
C
ode is onl
y
executed w
h
en a condition (or
conditions) evaluate to True
Provides a single application t
h
e abilit
y
to react
If
If
cond ition
Then
statement( s)
End If
Performs an operation onl
y
if t
h
e condition
evaluates to True
Used w
h
en an action is eit
h
er performed or not
If«Else
If
cond ition
Then
statement( s)
Else
statement( s)
End If
Performs t
h
e If portion onl
y
if t
h
e condition
evaluates to True and t
h
e Else portion ot
h
erwise.
Used in an eit
h
er or scenario w
h
en an operation
is alwa
y
s performed.
If«ElseIf
If cond ition1 Then statement( s) ElseIf cond ition2 Then statement( s) Else statement( s) End IfOnl
y
one section can be executed even if man
y
conditions
evaluate to True.
Used in a multiple c
h
oice scenario
Select
Select Case variable
statement( s) Case value1 statement( s) Case value2 statement( s) Case Else statement( s) End Select
Used to c
h
oose between 1 of several discrete values for a
variable.
Inclusion of
Case Else
is optional
Loops
Allows
y
ou to repeat running certain sections of
y
our
code
C
ode executes w
h
en a condition (or conditions)
evaluate to True
Be careful wit
h
Loops. T
h
e
y
can result in infinite
processing.
Forms
² Entr
y C
ondition
Entr
yonl
yw
hen a initial condition is met
² Iterated
Loops:
C
autionar
y
Items
1.
C
an t
h
is loop ever be entered
If no, t
hen
you don·t need t
he loop
2
.
C
an t
h
is loop ever be exited
Loop: Parts
1.
Wh
ere does t
h
e loop start?
Expressed as an assignment
2
.
Wh
en does t
h
e loop end?
Expressed as a condition
3
. How is t
h
e loop variable altered?
Expressed as an arit
hmetic operation
Wh
ile
W
hile
cond ition
statement( s)
W
end
Entr
y C
ondition Loop
Simplest form of t
h
e loop
Requires manual modification of t
h
e loop
For
F
or
variable
=start
To
finish [
Step
change]
statement( s)
N
ext
Iterated Loop
Favored because all t
h
e loop details are in t
h
e
Function
Fu
n
ction
functionName(parameter list)
statement( s)
functionName = value
End
Fun
ction
Block of code used to perform an operation
Bot
h
VBScript and QTP provide a large number of
built-in functions
Best Practices
C
omment liberall
y
Use a naming convention
Avoid mixing cases
You're Reading a Preview
Unlock full access with a free trial.
VBScript Examples
Greeting (printing to message box)
Greeting
2
(printing to current window)
Speed
Ch
eck (Dicision)
Blast Off! (FOR loop)
Var
y
ing Font (FOR loop)
Speed
Ch
eck
2
(event
h
andler)
Speed
Ch
eck
2
B (alternate code)
Get User Input
Heart Beat (calculation in event
h
andler)
Heart Beat
2
(arguments to event
h
andler)
Greeting
<html> <head> <title>VBScript Demo</title> </head> <body> <h1>VBScript Demo</h1> <script language="VBScript"> <!--Dim name Dim age name = "Jack" age = 20MsgBox "Hello, " & name & ". Are you " _ & age & " years old?³
-->
</script> </html>
You're Reading a Preview
Unlock full access with a free trial.
You're Reading a Preview
Unlock full access with a free trial.
You're Reading a Preview
Unlock full access with a free trial.
Var
y
ing Font (For Loop)
...
<body>
<h1>For Loop Demo</h1>
<script language="VBScript"> Dim num
For num = 1 To 6
document.write("<font color='red' size=" & _ num & ">")
document.write("<i>This sentence was´ & _
³ generated by VBScript.</i>") document.write("</font><br>") Next </script> </html>
Try it.
Events
Event :
An action b
y
t
h
e user--e.g., mouse-click,
mouse-move, etc.³t
h
at triggers execution of a
code known as
event hand ler
Event Handler :
Piece of programming code, written in
VBScript, Javascript, Java, etc., allowing t
h
e
Event Examples
Event Name
Event
Name
Example
Example
onClick
onClick
Mouse button clicked on textbox
Mouse button clicked on textbox
onDblClick
onDblClick
Button double--clicked on textbox
Button double
clicked on textbox
onMouseMove
onMouseMove
Mouse is moved
Mouse is moved
onMouseOut
onMouseOut
Mouse pointer leaves image area
Mouse pointer leaves image area
onMouseOver
onMouseOver
Mouse pointer first enters image area
Mouse pointer first enters image area
onLoad
You're Reading a Preview
Unlock full access with a free trial.
Subprogram
G
reeting
<html>
<head>
<script language="VBScript">
<!--Sub Greetings()
MsgBox "Welcome, " & theForm.myName.value
End Sub
-->
</script>
</head>
<body>
«
</body>
</html>
You're Reading a Preview
Unlock full access with a free trial.
You're Reading a Preview
Unlock full access with a free trial.
Speed
Ch
eck
2
(Event Handler)
<script language="VBScript"> <!--Sub CheckSpeed() Dim yourSpeed Dim diff Dim message const maxSpeed = 50 yourSpeed = 60message = "Your Speed is " & yourSpeed & " mph." diff = yourSpeed - maxSpeed
If diff > 0 Then
message = message & " You are speeding by " & diff & " mph." Else
message = message & "You are under the speed limit." End If
MsgBox message End Sub
-->
Speed
Ch
eck
2
B (Alternate
C
ode)
<html> <head> <title>VBScript Demo</title> <script language="VBScript"><!--I nsert Alternate code for event procedure here. --> </script> </head> <body> <h1>Speed Check</h1> <form name="myForm">
<input type="button" name="btnStart" value="Click Me"> </form>
</body> </html>
Get User Input
<html> <head>
<script language=³VBScript´>
. . . C ode for GreetUser() goes here
</script> </head> <body>
<h1>Get User Information Demo</h1> <form name="myForm">
Please enter your name.
<input type="text" name="myName" size="10"><br> Please enter your age.
<input type="text" name="myAge" size="3"><br> <p>
<input type="button" name="start" value="Click Me" onClick="GreetUser()">
</form> </body> </html>
You're Reading a Preview
Unlock full access with a free trial.
You're Reading a Preview
Unlock full access with a free trial.
GreetUser() Version
3
<script language=³VBScript> <!--Option Explicit Sub GreetUser() Dim messagemessage = "Welcome to the world of VBScript, " & _ myForm.myName.value & "!"
message = message & " Are you really " & _ myForm.myAge.value & " years old?"
document.write("<h1><font color='blue'>Welcome ³ & _ Message</font></h1>")
document.write(message) End Sub
- ->
You're Reading a Preview
Unlock full access with a free trial.
You're Reading a Preview
Unlock full access with a free trial.
You're Reading a Preview
Unlock full access with a free trial.
You're Reading a Preview
Unlock full access with a free trial.
You're Reading a Preview
Unlock full access with a free trial.
You're Reading a Preview
Unlock full access with a free trial.
You're Reading a Preview
Unlock full access with a free trial.
You're Reading a Preview
Unlock full access with a free trial.
You're Reading a Preview
Unlock full access with a free trial.