- The Fundamentals of VBScript
- VBScript Versus VB
- Creating Variables in VBScript
- Concatenating Strings
- Arrays and Loops
- Resizing Arrays
- Inequality Operators
- Conditional Statements
- Select Case Statements
- Sub
- Function
- Working with Arguments
- Beware of Types
- Event Procedure Naming Syntax
- Server-Side Events
- Local Variables
- Script-Level Variables
Event Procedure Naming Syntax
The syntax for naming an event procedure is
Sub ElementName_OnEventName() 'statements and stuff End Sub
Where
The Sub and End Sub keywords denote a procedure.
ElementName is the name of the HTML element or object that causes the event to be fired.
The underscore character (_) relates the element to the event.
On are the prefix characters that relate the event procedure to a given event.
EventName is the event the event procedure is capturing.
The following code is an example of the code skeleton that you would write to capture the Click event of a button named MyButton.
Sub MyButton_OnClick() 'Your code would be here End Sub
This is the code skeleton that you would write to capture a MouseOver event for an Image element named MyImage:
Sub MyImage_OnMouseOver() 'Your code would be here End Sub
The important thing to remember is that you must use the prefix, On to identify the event of the event procedure for which you are creating the code skeleton. The following example is the code skeleton that you would not create to capture the Click event of a button named BigButton.
Sub BigButton_Click() 'Your code would be here. But it 'wouldn't execute anyway. You forgot 'the On prefix for the Click. End Sub
A browser can fire many different events. The code in Listing 3.13 shows you a Web page in which there are two event procedures, one to capture a Click event for a button element and another to capture a MouseOver event for an image element. The output of the code is shown in Figure 3.9.
Listing 3.13: Using Click and MouseOver Events (03asp13.htm)
<HTML> <HEAD> <TITLE></TITLE> </HEAD> <SCRIPT LANGUAGE=VBScript> Sub image1_OnMouseOver() Dim s s = "You are moving over " & image1.name text1.value = s End Sub Sub button1_OnClick() Dim s s = "You have clicked a button" text1.value = s End Sub </SCRIPT> <BODY> <P><INPUT name=text1 size=30></P> <P> <INPUT type="button" value="Click Me" name=button1> </P> <P>Mouse your mouse over the blue box: </P> <P><INPUT type=Image src="box.gif" name=image1></P> <P> </P> </BODY> </HTML>
Figure 3.9 Event procedures provide real-time functionality to your Web applications.