VBScript

tacitmarigoldInternet et le développement Web

25 janv. 2014 (il y a 3 années et 7 mois)

415 vue(s)

VBScript Examples

Visual 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

VBScript 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)

Greeting

<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 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 (Decision)

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)

...

<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 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

Event 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

<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 Event

Subprogram
Greeting

<html>

<head>

<script language="VBScript">

<!
--

Sub Greetings()


MsgBox "Welcome, " & theForm.myName.value

End Sub

--
>

</script>

</head>

<body>



</body>

</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 Check

Speed Check 2 (Event 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 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 message

End Sub

--
>

</script>

Speed Check 2B (Alternate 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 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()

<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 message

End Sub

-

-
>

</script>

</head>


GreetUser() 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 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)

<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)

<!
--

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

--
>

</script>

Heart Beat 2 (Using 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 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 message

End Sub

--
>

</script>

onMouseOut

(Using 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 argument)

<head>



<script language="VBScript">

<!
--

Sub Greet(msg)


document.myForm.txtName.value = msg

End Sub
--
>

</script>


</head>

Three Buttons & One
Subprocedure

<head>

<script language="VBScript">

<!
--

--

Insert Subprogram ChangeBackground(color) here

End 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
Subprocedure

<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 Turn


Write an HTML page in which:


The user is asked to input in a form one’s name .


When a button is clicked, a greeting which is
appropriate for the current time

”Good morning,”
Good afternoon,” or “Good evening”

is displayed
along with the user’s name.

Your Turn (2)


Write an HTML page in which:


The user is asked to input in a form one’s 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 < $100, tax rate = 0%


For price >= $100, tax rate = 6%


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