Global Code

We've seen that code can be organized into functions, subroutines, and classes, and that some subroutines (and an occasional function) can be executed automatically if they are event handlers and the event they handle fires. However, that seems to offer a relatively limited "hook" for a script to run, and it doesn't seem to make it possible for a script to perform whatever initialization might be required in order for its event handlers to function successfully.

Global codethat is, code outside functions and subroutinesis the answer to this dilemma. It is executed automatically when the script loads or as the HTML on the page is parsed. The precise meaning of global code and the exact way in which it is executed depends on the host environment for which the script is written. We'll examine these in turn.

2.3.1 Active Server Pages

In ASP, global code is synonymous with code in direct ASP commandsit is script that is preceded by the <% or <%= tags and terminated by the %> tag. (For details on how script is embedded within in ASP page, see Chapter 5.) This code is executed automatically as the page's HTML is parsed.

It is also possible to include global code in tags in an ASP. However, this is not genuine global code; aside from variable declarations, the order in which this code is executed is undefined.

Figure 2-1 shows the web page produced by Example 2-7, which illustrates global code in an Active Server Page. Note that although the variable x is defined and assigned a value in global code within the


An Active Server Page

The current value of x is <%= x %>
<% Dim y y = 20 If x = 0 Then x = 10 %> Value returned by Increment function: <%= Increment(x) %>
Value returned by Increment function: <%= Increment(x) %>
Value returned by Decrement function: <%= Decrement(x) %>
The value of x is now <%= x %>. The value of y is <%= y %>.

Figure 2-1. The web page produced by Example 2-7


We can draw the following conclusions from Example 2-7:

  • Variable declarations placed at script level within the tags but not inside of functions, subroutines, and classes. All global code is executed by Internet Explorer, as Example 2-9 and Figure 2-3 show. In fact, global code can be used as a replacement for the Window_OnLoad event.

    Example 2-9. Global code for Internet Explorer


    Welcome to our web page!

    Figure 2-3. The document produced by Example 2-9


    2.3.4 Outlook Forms

    Like Windows Script Host, Outlook executes all global codenot just variable declarationswhen a form is loaded. In this case, global code corresponds closely to the Outlook form's Item_Open event procedure, which is fired when the form is opened.

    Although you can use global code for executable statements, in most cases it is preferable that you do not. Most Outlook form programming is event-driven; you should use events, including the Item_Open event, to handle variable initialization, and confine yourself to using global code to declare public and private variables.

    2 4 Reusable Code Libraries

Part I: The Basics


Program Structure

Data Types and Variables

Error Handling and Debugging

VBScript with Active Server Pages

Programming Outlook Forms

Windows Script Host 5.6

VBScript with Internet Explorer

Windows Script Components

Part II: Reference

Part III: Appendixes

Appendix A. Language Elements by Category

Appendix B. VBScript Constants

Appendix C. Operators

Appendix E. The Script Encoder

Vbscript in a Nutshell
VBScript in a Nutshell, 2nd Edition
ISBN: 0596004885
EAN: 2147483647
Year: 2003
Pages: 335 © 2008-2020.
If you may any questions please contact us: