Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "VBScript Examples. Visual Basic & VBScript Visual Basic Visual Basic Stand-alone (compiled or interpreted) Stand-alone (compiled or interpreted) Variables."— Presentation transcript:

1 VBScript Examples

2 Visual Basic & VBScript Visual Basic Visual Basic Stand-alone (compiled or interpreted) Stand-alone (compiled or interpreted) Variables are typed 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

3 VBScript 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 Input Get 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)

4 Greeting VBScript Demo VBScript Demo <!-- Dim name Dim age name = "Jack" age = 20 MsgBox "Hello, " & name & ". Are you " _ & age & " years old?“ --> Try it.

5 Greeting 2 VBScript Demo VBScript Demo <!-- Dim name Dim age name = "Jack" age = 20 document.Write "Hello, " & name & ". Are you " _ & age & " years old?“ --> Try it.

6 Speed Check 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 Try it. Speed Check (Decision)

7 Blast Off! (For Loop) VBScript Demo For Loop Demo Dim num For num = 10 To 1 Step -1 document.write(num & "!" & " ") Next document.write("Blast Off!") Try it.

8 Varying Font (For Loop)... For Loop Demo Dim num For num = 1 To 6 document.write(" ") document.write(" This sentence was” & _ “ generated by VBScript. ") document.write(" ") Next Try it.

9 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: 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 Handler: Piece of programming code, written in VBScript, Javascript, Java, etc., allowing the page to react to user input

10 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

11 <!-- -- Subprogram Greeting goes here -- --> onClick Demo Enter your name. <input type="button" value="Click Me" onClick="Greeting()"? OnClick Event

12 Subprogram Greeting <html><head> <!-- Sub Greetings() MsgBox "Welcome, " & theForm.myName.value MsgBox "Welcome, " & theForm.myName.value End Sub --></script></head> … …</body></html>

13 ... Speed Check 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 Try it. (Recall) Speed Check

14 Speed Check 2 (Event Handler) VBScript Demo Speed Check <input type="button" name="start" value="Click Me" onClick="CheckSpeed()"> Try it.

15 Speed Check 2 (Event Handler) <!-- 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 -->

16 Speed Check 2B (Alternate Code) VBScript Demo <!-- Insert Alternate code for event procedure here. --> Speed Check Try it.

17 Get User Input... Code for GreetUser() goes here Get User Information Demo Please enter your name. Please enter your age. <input type="button" name="start" value="Click Me" onClick="GreetUser()"> Try it.

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

19 GreetUser() Version 2 GreetUser() Version 2 <!-- 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 - -> Try it.

20 GreetUser() Version 3 GreetUser() Version 3 <!-- 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(" Welcome “ & _ Message ") document.write(message) End Sub - -> Try it.

21 Heart Beat (Calculaton) VBScript Calculaton Demo <!— Insert code for CalcuHeartBeat() here. --> How Many Times Have My Heart Beaten So Far? Please enter your age. <input type="button" name="btnStart" value="Calculate" onClick="CalcHeartBeat()"> Try it.

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

23 Heart Beat 2 (Using argument) VBScript Calculaton Demo <!— Insert code for CalcHeatBeat(years) here. --> How Many Times Have My Heart Beaten So Far? Please enter your age. <input type="button" name="btnStart" value="Calculate" onClick="CalcHeartBeat(myForm.txtAge.value)"> Try it.

24 Heart Beat 2 (Using argument) <!-- 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 -->

25 onMouseOut (Using argument) VBScript Calculaton Demo <!— Insert code for Greet(msg) here. --> onMouseOver & onMouseOut Demo Hover the cursor over the text box, then move it away. <input type="text" name="txtName" onMouseOver="Greet('Hello!')" onMouseOut="Greet('Good-bye!')"> Try it.

26 onMouseOut (Using argument) … <!-- Sub Greet(msg) document.myForm.txtName.value = msg End Sub -->

27 Three Buttons & One Subprocedure <!-- -- Insert Subprogram ChangeBackground(color) here End Sub --> Changing BG Color with One Subprocedure

28 Three Buttons & One Subprocedure … <!-- Sub ChangeBackground(color) document.write(" Demo ") document.write(" ") document.write(" Background Demo” _ & “ ") document.write(" ") End Sub -->

29 Your Turn Write an HTML page in which: Write an HTML page in which: The user is asked to input in a form one’s name. 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. 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.

30 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 name and a purchase price at a gift shop. 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: 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 = 0% For price >= $100, tax rate = 6% For price >= $100, tax rate = 6% A message box displays the user’s name, price, tax, and the total. A message box displays the user’s name, price, tax, and the total.


Download ppt "VBScript Examples. Visual Basic & VBScript Visual Basic Visual Basic Stand-alone (compiled or interpreted) Stand-alone (compiled or interpreted) Variables."

Similar presentations


Ads by Google