|
|
|
|
|
In the last lesson, you could see the below code for event-driven procedure. If you click the button on the page, you get the corresponding message. Internet Explorer defines the events available for form controls in the Internet Explorer Scripting Object Model documentation, which can be found on the Microsoft Web site (http://www.microsoft.com).
<HTML> <SCRIPT LANGUAGE="VBScript"> Sub btn1_onClick MsgBox "You clicked on me!" End Sub </SCRIPT> <body> <form> <input type="button" name="btn1" value="Click Me"> </form> </body> </HTML> Although the preceding way is probably the simplest and most general, you can attach VBScript code to events in other ways. You might define the procedure within the tag of the element. It is illustrated by the following example: <HTML> <SCRIPT LANGUAGE="VBScript"> Sub clkButton MsgBox "You clicked on me!" End Sub </SCRIPT> <body> <form> <input type="button" name="btn1" value="Click Me" onClick="clkButton"> </form> </body> </HTML> For short sections of code, Internet Explorer allows you to add inline code in the tag defining the control. For example, the following <INPUT> tag performs the same action as the previous code example when you click the button: <HTML> <body> <form> <input language="VBSCript" type="button" name="btn1" value="Click Me" onClick='MsgBox "You clicked on me!"'> </form> </body> </HTML> Notice that the function call itself is enclosed in single quotation marks, and the string for the MsgBox function is enclosed in double quotation marks. You can use multiple statements as long as you separate the statements with colons (:). You can also write a <SCRIPT> tag so that it applies only to a particular event for a specific control: <HTML> <SCRIPT LANGUAGE="VBScript" event="OnClick" for "btn1"> MsgBox "You clicked on me!" </SCRIPT> <body> <form> <input type="button" name="btn1" value="Click Me"> </form> </body> </HTML> Because the <SCRIPT> tag already specifies the event and the control, you don't use Sub and End Sub statements. Go To Page: 1
The copyright of the article Ways to Attach Code to Events in VB Script is owned by Maxim Karetnikov. Permission to republish Ways to Attach Code to Events in print or online must be granted by the author in writing.
|
|
|
|