We've now discussed all of the basic principles of structuring VBScript programs, of constructing subroutines that can be used by various parts of your program, of building functions that perform calculations and other manipulations and pass the result back to the calling part of the program, and of creating classes that allow you to encapsulate real-world processes and objects. The emphasis on subroutines, functions, and classes, though, raises another issuethat of code reuse. Typically, classes are defined so that they can be used in a variety of applications. Similarly, many subroutines and functions are intended not only to reduce code in a single application, but also to be "black boxes" that can provide some service to multiple applications.
Although it generally hasn't been emphasized and is dependent on the host platform, VBScript code can be reused on three of the four host platforms discussed here. The only platform that doesn't support code reuse isOutlook forms. That means that if you're scripting for WSH, ASP, or Internet Explorer, you can develop code libraries that you import into your script.
2.4.1 Active Server Pages
You can import HTML, client-side script, or server-side script into an ASP file by using the #include server-side directive. Its syntax is:
where PathType is one of the following keywords:
File
Indicates that sFileName is relative path from the current directory
Virtual
Indicates that sFileName is a full virtual path from the web server's root folder to the file to be included
and sFileName is the name of the file whose contents are to be included. Note that the #include directive must be surrounded by an HTML comment. The included file can consist of any combination of client-side script, server-side script, and HTML, as long as it is syntactically correct and consistent with the script or HTML source at the point in the ASP page at which it is inserted.
Examples Example 2-10 and Example 2-11 illustrate one possible use of the #include directive. Example 2-10 shows the contents of classes.inc, an include file that contains a class definition to be used by the ASP application. Example 2-11 shows the ASP page that includes the file. Note that the include file consists entirely of script and is delimited with the HTML tags (or the ASP <% and %> symbols). The ASP page in Example 2-11 inserts the contents of the include file in the HTML header, immediately after the tag.
Example 2-10. classes.inc, an include file
Example 2-11. An ASP page that uses an include file
<% Option Explicit %>
Including a Library File
Here is information about our server:
<% Dim oServer Set oServer = New CServer %> Name: <%= oServer.Name %>
Software: <%= oServer.Software %>
Port: <%= oServer.Port %>
Protocol <%= oServer.Protocol %>
Resource: <%= oServer.URL %>
The advantage of this approach is obvious: you can store common code in a separate file, making it available to all the ASP pages and all the ASP applications that require it. When that code requires modification, you need do so only once since there is only a single copy in a single location, rather than having to search through all of your web pages to discover which ones incorporate the code.
While reusable code libraries can be useful in ASP development, you should only include the code you actually need in your library. This is because there's a runtime cost associated with declaring a function for ASP. Including massive libraries in an ASP application tends to produce noticeable slowdowns in throughput.
2.4.2 Windows Script Host
Although standard Windows Script host files (i.e., .vbs files) do not allow you to import other files, WSH files with XML elements (i.e., .wsf files) do. Include another file by using the
Note that files must be included on a per-job basis. In other words, if a .wsf file contains multiple jobs, you must have a separate
where sURL is the URL of the include file and sLanguage is the language in which the file designated by sURL is written. sLanguage can be "VBScript" or any other valid scripting language.
The include file is simply inserted into the text stream on the client at the point that the tag.
Example 2-14. Validate.inc, an include file
Private Function IsBlank(sValue) If Trim(sValue) = "" Then IsBlank = True Else IsBlank = False End If End Function
Example 2-15. A web page that uses an include file
The SRC Attribute
Name:
Address:
City: State Zip Code
Chapter 3 Data Types and Variables |
Part I: The Basics
Introduction
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