Reusable Code Libraries: Client-Side Scripts for MSIE
Printer Friendly Version
The Script Level
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.
Active Server Pages
In ASP, script-level code is synonymous with code in direct ASP
commands--it 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,
VBScript with Active Server Pages.) This code
is executed automatically as the page's HTML is parsed.
It is also possible to include script-level code in <SCRIPT>...</SCRIPT> 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.
Figure
2-1 shows the web page produced by Example
2-7, which illustrates script-level code in an Active Server Page. Note
that although the variable x is defined and assigned
a value at script level within the <SCRIPT>
tag, the variable declaration is recognized but the variable assignment isn't.
We can determine this because we've used the Option
Explicit statement to require variable declaration,
but the VBScript language engine did not raise an error when it first
encountered the use of x on the second line after the
<BODY> tag. But our assignment of 10 to x is not recognized, since the second line of our web
page strongly suggests that x is
uninitialized.
Example
2-7: Script-Level Code in an Active Server Page
<% Option Explicit %>
<HEAD>
<TITLE>Script-level code in ASP</TITLE>
<SCRIPT LANGUAGE="VBScript" RUNAT="Server">
Private x
x = 10
Private Function Increment(lVar)
lVar = lVar + 1
Increment = lVar
End Function
Private Function Decrement(lVar)
lVar = lVar - 1
Decrement = lVar
End Function
</SCRIPT>
</HEAD>
<BODY>
<H2><CENTER>An Active Server Page</CENTER></H2>
The current value of x is <%= x %> <BR>
<%
Dim y
y = 20
If x = 0 Then x = 10
%>
Value returned by Increment function: <%= Increment(x) %> <BR>
Value returned by Increment function: <%= Increment(x) %> <BR>
Value returned by Decrement function: <%= Decrement(x) %> <BR>
The value of <I>x</I> is now <%= x %>.
The value of <I>y</I> is <%= y %>.
</BODY>
</HTML>
The conclusions to be drawn from Example
2-7 are unusually clear:
- Variable declarations placed at script level within the
<SCRIPT> tag are recognized by ASP.
- Aside from variable declarations, no script-level code
should be placed within the
<SCRIPT> tag.
The remaining code located within a <SCRIPT> tag should consist solely of function,
subroutine, and class definitions.
- Direct commands can contain any script-level code.
- All direct commands are
executed as the web server is parsing the HTML and generating a response
to the client. In other words, along with handlers for the events
supported by ASP (Application_OnStart, Application_OnEnd,
Session_OnStart, Session_OnEnd, OnTransactionAbort, and
OnTransactionCommit), direct commands are basic "hooks" that allow your
code to run.
Printer Friendly Version