Title: VBScript Examples
1VBScript Examples
2Visual Basic VBScript
- Visual Basic
- Stand-alone (compiled or interpreted)
- Variables are typed
- VBScript
- Embedded in a Web page and executed by browser
- Variables can be associated with any type
- Uses most program structures available in VB,
including loops and decisions
3VBScript Examples
- Greeting (printing to message box)
- Greeting 2 (printing to current window)
- Speed Check (Dicision)
- Blast Off! (FOR loop)
- Varying Font (FOR loop)
- Speed Check 2 (event handler)
- Speed Check 2B (alternate code)
- Get User Input
- Heart Beat (calculation in event handler)
- Heart Beat 2 (arguments to event handler)
4Greeting
lthtmlgt ltheadgt lttitlegtVBScript
Demolt/titlegt lt/headgt ltbodygt lth1gtVBScript
Demolt/h1gt ltscript language"VBScript"gt lt!-- Dim
name Dim age name "Jack" age 20
MsgBox "Hello, " name ". Are you " _
age " years old? --gt lt/scriptgt lt/htmlgt
Try it.
5Greeting 2
lthtmlgt ltheadgt lttitlegtVBScript
Demolt/titlegt lt/headgt ltbodygt lth1gtVBScript
Demolt/h1gt ltscript language"VBScript"gtlt!-- Dim
name Dim age name "Jack" age 20
document.Write "Hello, " name ". Are you "
_ age " years old? --gt
lt/scriptgtlt/htmlgt
Try it.
6ltbodygt lth1gtSpeed Checklt/h1gt ltscript
language"VBScript"gt Dim yourSpeed Dim diff
Dim message const maxSpeed 50 yourSpeed
60 message "Your Speed is " yourSpeed "
mph." diff yourSpeed - maxSpeed If diff gt 0
Then message message " You are speeding
by " diff " mph." Else message
message "You are under the speed limit." End
If MsgBox message lt/scriptgt lt/bodygt lt/htmlgt
Speed Check (Decision)
Try it.
7Blast Off! (For Loop)
lthtmlgt ltheadgt lttitlegtVBScript
Demolt/titlegt lt/headgt ltbodygt lth1gtFor Loop
Demolt/h1gt ltscript language"VBScript"gt Dim
num For num 10 To 1 Step -1
document.write(num "!" "ltbrgt") Next
document.write("Blast Off!") lt/scriptgt lt/htmlgt
Try it.
8Varying Font (For Loop)
... ltbodygt lth1gtFor Loop Demolt/h1gt ltscript
language"VBScript"gt Dim num For num 1 To
6 document.write("ltfont color'red' size"
_ num "gt")
document.write("ltigtThis sentence was _
generated by VBScript.lt/igt")
document.write("lt/fontgtltbrgt")
Next lt/scriptgt lt/htmlgt
Try it.
9Events
- Event An action by the user--e.g., mouse-click,
mouse-move, etc.that triggers execution of a
code known as event handler - Event Handler Piece of programming code,
written in VBScript, Javascript, Java, etc.,
allowing the page to react to user input
10Event Examples
Event Name Example
onClick Mouse button clicked on textbox
onDblClick Button double-clicked on textbox
onMouseMove Mouse is moved
onMouseOut Mouse pointer leaves image area
onMouseOver Mouse pointer first enters image area
onLoad Web page is loaded
11OnClick Event
ltheadgt ltscript language"VBScript"gt lt!-- --
Subprogram Greeting goes here -- --gt lt/scriptgt lt/h
eadgt ltbodygt lth1gtonClick Demolt/h1gt ltform
name"theForm"gt Enter your name. ltinput
type"text" name"myName" size20"gt ltinput
type"button" value"Click Me"
onClick"Greeting()"? lt/formgt lt/bodygt
12Subprogram Greeting
- lthtmlgt
- ltheadgt
- ltscript language"VBScript"gt
- lt!--
- Sub Greetings()
- MsgBox "Welcome, " theForm.myName.value
- End Sub
- --gt
- lt/scriptgt
- lt/headgt
- ltbodygt
- lt/bodygt
- lt/htmlgt
13(Recall) Speed Check
. . .ltbodygt lth1gtSpeed Checklt/h1gt ltscript
language"VBScript"gt Dim yourSpeed Dim diff
Dim message const maxSpeed 50 yourSpeed
60 message "Your Speed is " yourSpeed "
mph." diff yourSpeed - maxSpeed If diff gt 0
Then message message " You are speeding
by " diff " mph." Else message
message "You are under the speed limit." End
If MsgBox message lt/scriptgt lt/bodygt lt/htmlgt
Try it.
14Speed Check 2 (Event Handler)
lthtmlgt ltheadgt lttitlegtVBScript Demolt/titlegt ltscript
language"VBScript"gt lt! Insert event
procedure code here.--gt lt/scriptgt lt/headgt ltbod
ygt lth1gtSpeed Checklt/h1gt ltform name"myForm"gt
ltinput type"button" name"start" value"Click
Me" onClick"CheckSpeed()"gt lt/formgt lt/
bodygt lt/htmlgt
Try it.
15Speed Check 2 (Event Handler)
ltscript language"VBScript"gt lt!-- Sub
CheckSpeed() Dim yourSpeed Dim diff Dim
message const maxSpeed 50 yourSpeed 60
message "Your Speed is " yourSpeed " mph."
diff yourSpeed - maxSpeed If diff gt 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 --gt lt/scriptgt
16Speed Check 2B (Alternate Code)
lthtmlgt ltheadgt lttitlegtVBScript Demolt/titlegt ltscript
language"VBScript"gt lt!-- Insert Alternate
code for event procedure here. --gt lt/scriptgt lt/hea
dgt ltbodygt lth1gtSpeed Checklt/h1gt ltform
name"myForm"gt ltinput type"button"
name"btnStart" value"Click Me"gt lt/formgt lt/bodygt
lt/htmlgt
Try it.
17Get User Input
lthtmlgtltheadgt ltscript languageVBScriptgt. . .
Code for GreetUser() goes here lt/scriptgtlt/headgt
ltbodygt lth1gtGet User Information Demolt/h1gt ltform
name"myForm"gt Please enter your name.
ltinput type"text" name"myName" size"10"gtltbrgt
Please enter your age. ltinput type"text"
name"myAge" size"3"gtltbrgt ltpgt ltinput
type"button" name"start" value"Click Me"
onClick"GreetUser()"gt lt/formgt lt/bodygt lt/htmlgt
Try it.
18GreetUser()
ltheadgtltscript languageVBScriptgtlt!-- Option
Explicit Sub GreetUser() Dim message message
"Welcome to the world of VBScript, " _
myForm.myName.value "!" message
message " Are you really " _
myForm.myAge.value " years old?" MsgBox
message End Sub - -gt lt/scriptgt lt/headgt
19 GreetUser() Version 2
ltscript languageVBScriptgtlt!-- Option
Explicit Sub GreetUser() Dim message
message "Welcome to the world of VBScript, "
_ myForm.myName.value "!"
message message " Are you really " _
myForm.myAge.value " years old?"
document.Write (message) End Sub- -gt lt/scriptgt
Try it.
20 GreetUser() Version 3
ltscript languageVBScriptgtlt!-- Option
Explicit Sub GreetUser() Dim message message
"Welcome to the world of VBScript, " _
myForm.myName.value "!" message
message " Are you really " _
myForm.myAge.value " years old?"
document.write("lth1gtltfont color'blue'gtWelcome
_ Messagelt/fontgtlt/h1gt")
document.write(message) End Sub- -gt lt/scriptgt
Try it.
21Heart Beat (Calculaton)
lthtmlgt ltheadgt lttitlegtVBScript Calculaton
Demolt/titlegt ltscript language"VBScript"gt lt!
Insert code for CalcuHeartBeat()
here. --gt lt/scriptgt lt/headgt ltbodygt lth2gtHow Many
Times Have My Heart Beaten So Far?lt/h2gt ltform
name"myForm"gt Please enter your age. ltinput
type"text" name"txtAge" size"3"gtltbrgt ltinput
type"button" name"btnStart" value"Calculate"
onClick"CalcHeartBeat()"gt lt/formgt lt/bodygt
lt/htmlgt
Try it.
22Heart Beat (Calculaton)
lt!-- Sub CalcHeartBeat() const daysPerYear
365 const hoursPerDay 24 const
minutesPerHour 60 const beatsPerMin 70
Dim totalBeats Dim years years
document.myForm.txtAge.value totalBeats years
daysPerYear hoursPerDay _
minutesPerHour beatsPerMin message "If
your are " years " years old, " _
"then your heart has beaten " totalBeats "
times so far." MsgBox message End
Sub --gt lt/scriptgt
23Heart Beat 2 (Using argument)
lthtmlgt ltheadgt lttitlegtVBScript Calculaton
Demolt/titlegt ltscript language"VBScript"gt lt!
Insert code for CalcHeatBeat(years)
here. --gt lt/scriptgt lt/headgt ltbodygt lth2gtHow Many
Times Have My Heart Beaten So Far?lt/h2gt ltform
name"myForm"gt Please enter your age. ltinput
type"text" name"txtAge" size"3"gtltbrgt ltinput
type"button" name"btnStart" value"Calculate"
onClick"CalcHeartBeat(myForm.txtAge.value)
"gt lt/formgt lt/bodygt lt/htmlgt
Try it.
24Heart Beat 2 (Using argument)
ltscript language"VBScript"gt lt!-- Sub
CalcHeartBeat(years) const daysPerYear 365
const hoursPerDay 24 const minutesPerHour
60 const beatsPerMin 70 Dim totalBeats
totalBeats years daysPerYear hoursPerDay
_ minutesPerHour beatsPerMin
message "If your are " years " years old, "
_ "then your heart has beaten "
totalBeats " times so far." MsgBox
message End Sub --gt lt/scriptgt
25onMouseOut (Using argument)
lthtmlgt ltheadgt lttitlegtVBScript Calculaton
Demolt/titlegt ltscript language"VBScript"gt lt!
Insert code for Greet(msg) here. --gt lt/scriptgt lt/h
eadgt ltbodygt lth1gtonMouseOver onMouseOut
Demolt/h1gt ltform namemyFormgt Hover the cursor
over the text box, then move it away.ltbrgt ltinput
type"text" name"txtName"
onMouseOver"Greet('Hello!')"
onMouseOut"Greet('Good-bye!')"gt
lt/formgt lt/bodygt lt/htmlgt
Try it.
26onMouseOut (Using argument)
ltheadgt ltscript language"VBScript"gt lt!-- Sub
Greet(msg) document.myForm.txtName.value
msg End Sub --gt lt/scriptgt lt/headgt
27Three Buttons One Subprocedure
ltheadgt ltscript language"VBScript"gt lt!-- --
Insert Subprogram ChangeBackground(color)
here End Sub --gt lt/scriptgt lt/headgt ltbodygt lth1gtCha
nging BG Color with One Subprocedurelt/h1gt ltformgt lt
input type"button" value"To Blue Background "
onClick"ChangeBackground('blue')"gt
ltinput type"button" value"To Yellow Background"
onClick"ChangeBackground('yellow')"gt
ltinput type"button" value"To Red Background
onClick"ChangeBackground('red')"gt lt/
bodygt
28Three Buttons One Subprocedure
ltheadgt ltscript language"VBScript"gt lt!-- Sub
ChangeBackground(color) document.write("lthtmlgtlt
headgtlttitlegtDemolt/titlegtlt/headgt")
document.write("ltbody bgcolor'" color "'gt")
document.write("lth1gtltfont color'white'gtBackgrou
nd Demo _
lt/fontgtlt/h1gt") document.write("lt/bodygtlt/htmlgt"
) End Sub --gt lt/scriptgt lt/headgt
29Your Turn
- Write an HTML page in which
- The user is asked to input in a form ones name .
- When a button is clicked, a greeting which is
appropriate for the current timeGood morning,
Good afternoon, or Good eveningis displayed
along with the users name.
30Your Turn (2)
- Write an HTML page in which
- The user is asked to input in a form ones name
and a purchase price at a gift shop. - When a button is clicked, the luxury tax on the
price is calculated according to the following
formula - For price lt 100, tax rate 0
- For price gt 100, tax rate 6
- A message box displays the users name, price,
tax, and the total.