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 script to run, nor does it seem to make it possible for a script to perform whatever initialization might be required in order for its event handlers to function successfully.
The script level is the answer to this dilemma. Script-level code—that is, code outside functions and subroutines—is executed automatically when the script loads or as the HTML on the page is parsed. The precise meaning of script-level code and the exact way in which code at script level is executed depends on the host environment for which the script is written. We’ll examine these in turn.
In ASP, script-level code is synonymous
with code in direct ASP commands—it is script that is preceded
<%= 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 script-level code in
tags in an ASP. However, this is not
genuine script-level code: aside from variable declarations, the
order in which this code is executed is undefined.