VBScript Examples. Visual Basic & VBScript Visual Basic Visual Basic Stand-alone (compiled or...

Preview:

Citation preview

VBScript VBScript ExamplesExamples

Visual Basic & VBScriptVisual Basic & VBScript

Visual BasicVisual Basic Stand-alone Stand-alone

(compiled or (compiled or interpreted)interpreted)

Variables are typedVariables are typed

VBScriptVBScript Embedded in a Web Embedded in a Web

page and executed page and executed by browserby browser

Variables can be Variables can be associated with any associated with any typetype

Uses most program Uses most program structures available structures available in VB, including in VB, including loops and decisionsloops and decisions

VBScript ExamplesVBScript Examples Greeting (printing to message box)Greeting (printing to message box) Greeting 2 (printing to current window)Greeting 2 (printing to current window) Speed Check (Dicision)Speed Check (Dicision) Blast Off! (FOR loop)Blast Off! (FOR loop) Varying Font (FOR loop)Varying Font (FOR loop) Speed Check 2 (event handler)Speed Check 2 (event handler) Speed Check 2B (alternate code)Speed Check 2B (alternate code) Get User InputGet User Input Heart Beat (calculation in event handler)Heart Beat (calculation in event handler) Heart Beat 2 (arguments to event handler)Heart Beat 2 (arguments to event handler)

GreetingGreeting<html><head> <title>VBScript Demo</title></head><body><h1>VBScript Demo</h1><script language="VBScript"><!-- Dim name Dim age

name = "Jack" age = 20 MsgBox "Hello, " & name & ". Are you " _ & age & " years old?“--> </script></html>

Try it.

Greeting 2Greeting 2<html><head> <title>VBScript Demo</title></head><body><h1>VBScript Demo</h1><script language="VBScript"><!-- Dim name Dim age

name = "Jack" age = 20 document.Write "Hello, " & name & ". Are you " _ & age & " years old?“--> </script></html>

Try it.

<body><h1>Speed Check</h1><script language="VBScript"> Dim yourSpeed Dim diff Dim message const maxSpeed = 50

yourSpeed = 60 message = "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</script></body></html> Try it.

Speed Check Speed Check (Decision)(Decision)

Blast Off! (For Loop)Blast Off! (For Loop)<html><head> <title>VBScript Demo</title></head>

<body><h1>For Loop Demo</h1><script language="VBScript"> Dim num

For num = 10 To 1 Step -1 document.write(num & "!" & "<br>") Next document.write("Blast Off!")</script></html>

Try it.

Varying Font (For Loop)Varying 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.

EventsEvents

EventEvent: : An action by the user--e.g., mouse-click, An action by the user--e.g., mouse-click, mouse-move, etc.—that triggers mouse-move, etc.—that triggers execution of a code known as execution of a code known as event event handlerhandler

Event HandlerEvent Handler: : Piece of programming code, written in Piece of programming code, written in VBScript, Javascript, Java, etc., allowing VBScript, Javascript, Java, etc., allowing the page to react to user inputthe page to react to user input

Event ExamplesEvent Examples

Event Event NameName

ExampleExample

onClickonClick Mouse button clicked on textboxMouse button clicked on textbox

onDblClickonDblClick Button double-clicked on textboxButton double-clicked on textbox

onMouseMoveonMouseMove Mouse is movedMouse is moved

onMouseOutonMouseOut Mouse pointer leaves image areaMouse pointer leaves image area

onMouseOveronMouseOver Mouse pointer first enters image areaMouse pointer first enters image area

onLoadonLoad Web page is loadedWeb page is loaded

<head><script language="VBScript"><!-- -- Subprogram Greeting goes here ----></script></head>

<body> <h1>onClick Demo</h1> <form name="theForm"> Enter your name. <input type="text" name="myName" size=20"> <input type="button" value="Click Me" onClick="Greeting()"? </form></body>

OnClick OnClick EventEvent

Subprogram Subprogram GreetingGreeting<html><html><head><head><script language="VBScript"><script language="VBScript"><!--<!--Sub Greetings()Sub Greetings() MsgBox "Welcome, " & theForm.myName.valueMsgBox "Welcome, " & theForm.myName.valueEnd SubEnd Sub-->--></script></script></head></head><body><body>

……</body></body></html></html>

. . .<body><h1>Speed Check</h1><script language="VBScript"> Dim yourSpeed Dim diff Dim message const maxSpeed = 50

yourSpeed = 60 message = "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</script></body></html> Try it.

(Recall) Speed (Recall) Speed CheckCheck

Speed Check 2 (Event Speed Check 2 (Event Handler)Handler)

<html><head><title>VBScript Demo</title><script language="VBScript"><!— Insert event procedure code here.--> </script></head>

<body><h1>Speed Check</h1><form name="myForm"> <input type="button" name="start" value="Click Me" onClick="CheckSpeed()"></form></body></html>

Try it.

Speed Check 2 (Event Speed Check 2 (Event Handler)Handler)

<script language="VBScript"><!--Sub CheckSpeed() Dim yourSpeed Dim diff Dim message const maxSpeed = 50

yourSpeed = 60 message = "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 messageEnd Sub--></script>

Speed Check 2B (Alternate Speed Check 2B (Alternate Code)Code)

<html><head><title>VBScript Demo</title><script language="VBScript"><!-- Insert 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>

Try it.

Get User InputGet User Input<html><head> <script language=“VBScript”>. . . Code 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>

Try it.

GreetUser()GreetUser()

<head><script language=“VBScript><!--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 messageEnd Sub- -></script></head>

GreetUser() Version 2GreetUser() Version 2

<script language=“VBScript><!--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- -></script>

Try it.

GreetUser() Version 3GreetUser() Version 3<script language=“VBScript><!--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("<h1><font color='blue'>Welcome “ & _ Message</font></h1>") document.write(message)End Sub- -></script> Try it.

Heart Beat (Calculaton)Heart Beat (Calculaton)<html><head><title>VBScript Calculaton Demo</title><script language="VBScript"><!— Insert code for CalcuHeartBeat() here.--></script></head>

<body><h2>How Many Times Have My Heart Beaten So Far?</h2><form name="myForm"> Please enter your age. <input type="text" name="txtAge" size="3"><br> <input type="button" name="btnStart" value="Calculate" onClick="CalcHeartBeat()"></form></body></html>

Try it.

Heart Beat (Calculaton)Heart Beat (Calculaton)<!--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 messageEnd Sub--></script>

Heart Beat 2 (Using Heart Beat 2 (Using argument)argument)

<html><head><title>VBScript Calculaton Demo</title><script language="VBScript"><!— Insert code for CalcHeatBeat(years) here.--></script></head>

<body><h2>How Many Times Have My Heart Beaten So Far?</h2><form name="myForm"> Please enter your age. <input type="text" name="txtAge" size="3"><br> <input type="button" name="btnStart" value="Calculate" onClick="CalcHeartBeat(myForm.txtAge.value)"></form></body></html>

Try it.

Heart Beat 2 (Using Heart Beat 2 (Using argument)argument)

<script language="VBScript"><!--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 messageEnd Sub--></script>

onMouseOut (Using onMouseOut (Using argument)argument)

<html><head><title>VBScript Calculaton Demo</title><script language="VBScript"><!— Insert code for Greet(msg) here.--></script></head>

<body><h1>onMouseOver & onMouseOut Demo</h1><form name=“myForm”>Hover the cursor over the text box, then move it away.<br> <input type="text" name="txtName" onMouseOver="Greet('Hello!')" onMouseOut="Greet('Good-bye!')"> </form></body></html> Try it.

onMouseOut (Using onMouseOut (Using argument)argument)

<head>… <script language="VBScript"><!--Sub Greet(msg) document.myForm.txtName.value = msgEnd Sub --></script>

</head>

Three Buttons & One Three Buttons & One SubprocedureSubprocedure

<head><script language="VBScript"><!---- Insert Subprogram ChangeBackground(color) hereEnd Sub--></script></head>

<body><h1>Changing BG Color with One Subprocedure</h1><form><input type="button" value="To Blue Background " onClick="ChangeBackground('blue')"><input type="button" value="To Yellow Background" onClick="ChangeBackground('yellow')"><input type="button" value="To Red Background “ onClick="ChangeBackground('red')"></body>

Three Buttons & One Three Buttons & One SubprocedureSubprocedure

<head>… <script language="VBScript"><!--Sub ChangeBackground(color) document.write("<html><head><title>Demo</title></head>") document.write("<body bgcolor='" & color & "'>") document.write("<h1><font color='white'>Background Demo” _ & “</font></h1>") document.write("</body></html>")End Sub--></script></head>

Your TurnYour Turn

Write an HTML page in which:Write an HTML page in which: The user is asked to input in a form The user is asked to input in a form

one’s name .one’s name . When a button is clicked, a greeting When a button is clicked, a greeting

which is appropriate for the current which is appropriate for the current time—”Good morning,” Good time—”Good morning,” Good afternoon,” or “Good evening”—is afternoon,” or “Good evening”—is displayed along with the user’s name.displayed along with the user’s name.

Your Turn (2)Your Turn (2)

Write an HTML page in which:Write an HTML page in which: The user is asked to input in a form one’s The user is asked to input in a form one’s

name and a purchase price at a gift shop.name and a purchase price at a gift shop. When a button is clicked, the luxury tax When a button is clicked, the luxury tax

on the price is calculated according to the on the price is calculated according to the following formula:following formula: For price < $100, tax rate = 0%For price < $100, tax rate = 0% For price >= $100, tax rate = 6%For price >= $100, tax rate = 6%

A message box displays the user’s name, A message box displays the user’s name, price, tax, and the total.price, tax, and the total.

Recommended