|
|
|
|
|
|
|
|
Your First VBScript ExerciseThe easiest way to learn any language is to work with it. So let's get right into exercise 1 and expose you to the process of using VBScript in your web pages. Just follow along with the step-by-step instructions to create your first script-enabled web page. Exercise 1: Adding VBScript to a Web pageA completed copy of this part of the exercise can be found in the file exer1_v1.html. In this exercise, you will create an HTML document and add a simple script to respond to a click event generated by a command button. You will need to be familiar with creating and testing an HTML document. Creating the HTML DocumentOpen up a text editor application and insert the following HTML code:
Save the file and test it by loading it into Internet Explorer. The resulting page should be similar to the figure below. Try out the Click Me button. Does anything happen? In the next part we will add a script to provide functionality for the Click Me command button. Adding VBScriptA completed copy of this part of the exercise can be found in the file exer1_v2.html. Re-open the HTML document that you created in part 1, if necessary. Modify the document adding the lines shown with shading below:
Save the file and test it by loading it into Internet Explorer. Try out the Click Me button. The result is shown below:
How It WorksLet's take a look at the three lines of code that you added. We want you to have a firm understanding of what the VBScript code is doing and how it is implemented within the HTML document. The first line defines a script. The FOR argument specifies that this script is for the button named cmdClickMe, the name we have given our command button with the HTML <INPUT> tag. The EVENT argument says that this script should be run when the button is clicked. The LANGUAGE argument states that this is a VBScript module.
The second line is the only line of VBScript in this HTML document. The MsgBox function simply displays a message dialog. You will see more of the MsgBox function later in this tutorial. The third line marks the end of our script. In the previous part, we simply inserted the VBScript module right after the HTML tag that defined the command button. While this method is functional, it is not the preferred approach. HTML by itself can be confusing to read with all of its tags and text. Adding VBScript into the middle all of this just makes it even more complicated. A more organized alternative is to place all of your script together within the HTML document. The following steps introduce you to this approach. Preferred Method to Include VBScriptA completed copy of this part of the exercise can be found in the file exer1_v3.html.
Save the file and test the file by loading it into Internet Explorer. When you try out the Click Me button, the result is the same as the previous example. How It WorksThis second method starts with the same <SCRIPT> tag as the previous example. At the center of this script are three lines that provide the functionality for our page. The first line defines a sub-procedure called cmdClickMe_OnClick. This will be executed any time that the control cmdClickMe is clicked. This type of procedure is referred to as an event procedure. The event is the user clicking the button. The procedure that we associate with this event is executed every time the button is clicked.
On the second line we find the MsgBox function again, while the third line marks an end to our subroutine. Don't get too hung up on understanding all of the details of this right now, you will see plenty more examples along the way. SummaryThat's it-you just created your first VBScript-enabled web page. Along the way you have learned:
Next up we will look at what VBScript has to offer in the way of variables.
VBScript Tutorial
Contents· What is VBScript? · How to Use this Tutorial Lesson 1 · Adding VBScript to Web Pages · The <SCRIPT> Tag · Non-Supporting Browsers · Your 1stirst VBScript Exercise Lesson 2 · Working with Variables · Declaring Variables · Scope of Variables · Constants · Arrays · Your 2nd VBScript Exercise Lesson 3 · Objects and VBScript · Adding Objects to Web Pages · Linking VBScript with Objects · Your 3rd VBScript Exercise Lesson 4 · Controlling VBScript Routines · Conditional Statements · Looping Statements · Your 4th VBScript Exercise Lesson 5 · Using VBScript with Forms · Validating Your Forms · Your 5th VBScript Exercise · Summary
|