Modules

Although there is no explicitly defined code module in VBScript, the <SCRIPT></SCRIPT> tags define a code module in the same way that the <FORM></FORM> tags define a form. Code modules are meaningful in VBScript because they allow scoping of variables and procedures at the module level. Scoping is the process that determines how long a variable or procedure exists and what parts of the application can use the variable or procedure.

Variables

There are two levels of scope for any variable in VBScript: Script and Procedure. Script-level variables are declared with the Dim keyword inside a module but outside a procedure. Script-level variables are available only to the module in which they are defined.

You can also declare Procedure-level variables. Procedure-level variables are declared inside a procedure, with the Dim keyword. Procedure-level variables are available only to the procedure in which they are defined. Here's an example that shows the declaration of each of these types of variables:

<SCRIPT LANGUAGE="VBScript">

    `Script-level variable
    Dim ScriptVar

    Sub MySub
        `Procedure-level variable
        Dim ProcVar
    End Sub
</SCRIPT> 

Modules defined in a script section also support the Option Explicit statement, which was discussed earlier. When Option Explicit is used in a module, it must appear as the first line of the module.

Functions and Subroutines

Function and subroutine procedures in VBScript are scoped at only one level: Page. Any procedure that you create in VBScript can be accessed from any other portion of your application. Procedures are not qualified with the Public or Private keyword.

Event Procedures

Event procedures can be implemented in VBScript in several different ways. (An event procedure is a procedure that gets executed when an event, such as a mouse click, occurs.) Event procedures can be defined by simply coding them into VBScript directly, using attributes of the <INPUT> tag or attributes of the <SCRIPT></SCRIPT> tags.

Placing event procedures in a module is just a matter of adding a coding structure with the syntax Sub control_eventname to the module. This is the most common structure and the one most familiar to programmers of Visual Basic for Applications. The following code shows the definition of the OnClick event for a button named btnMine:

<SCRIPT LANGUAGE="VBScript">
    Sub btnMine_OnClick
        MsgBox "Event Text"
    End Sub
</SCRIPT>
<FORM>
<INPUTNAME="btnMine" TYPE="BUTTON" VALUE="Click Here">
</FORM> 

The <SCRIPT></SCRIPT> tags also have attributes, however, that allow a module to explicitly support only one event. The following code shows the code designed to support only the OnClick event for btnMine:

<SCRIPT LANGUAGE="VBScript" FOR="btnMine" EVENT="OnClick">
    MsgBox "Event Test"
</SCRIPT>
<FORM>
<INPUT>NAME="btnMine" TYPE="BUTTON" VALUE="Click Here">
</FORM> 

Finally, you can attach code to controls by using the appropriate attribute of the control itself. The following code shows how VBScript code can be attached to a button OnClick event:

<SCRIPT LANGUAGE="VBScript">
</SCRIPT>
<FORM>
<INPUT TYPE="BUTTON" OnClick="MsgBox `Event Test'"
    VALUE="Click Here">
</FORM> 

Immediate Execution

Although VBScript is generally organized into functions and subroutines, such procedures are not required. VBScript code can actually be executed inline if code is placed directly in between the <SCRIPT></SCRIPT> tags. The following code declares a variable and initializes its value automatically when the page is loaded:

<SCRIPT LANGUAGE="VBScript">
    Dim MyVar
    MyVar=100
</SCRIPT> 

Notice that the code is not contained between Function…End Function or Sub…End Sub keywords. When code is created this way, it is executed when the page loads, and never again. This coding technique is known as immediate execution. Immediate execution is useful for initializing data or for dynamically changing a web page when it is loaded.

KEY CONCEPT:Code that is not contained in a function or subroutine is executed when the page is loaded. This is called immediate execution.

© 1996 by Scot Hillier. All rights reserved.