Writing Reusable Script

See Also

In most Web applications, you'll want to display blocks of HTML content on multiple pages or process script in multiple files.

To use content in multiple files

You can use the #INCLUDE directive to:

Note   When you insert the contents of other files into your page, the Script Outline window does not reflect their contents. Similarly, objects that are defined in the included file do not appear in the IntelliSense® statement completion drop-down list.

Sharing HTML Content

Your application might contain HTML elements that you want to use on multiple pages: page navigation buttons (Next, Previous, Home), copyright information, a company logo, and so on.

Instead of copying and pasting the text into multiple pages, you can place the information into files, and then reference the files that contain the desired text at the appropriate places in other your pages. Then if you need to update the text, you can change it in the file and all the pages that include the file automatically display the updated text.

To reference information that is in another file

For example, the following lines include a file named Header.inc at the top of a page and a file named Footer.inc at the bottom of a page:

<!-- #INCLUDE FILE="Header.inc" -->

Normal HTML text and script goes here.

<!-- #INCLUDE FILE="Footer.inc" -->

When the file is processed by the Web server, the entire contents of the Header.inc and Footer.inc files are inserted into the file at the location of the #INCLUDE directive. Any script in Header.inc and Footer.inc is processed after all includes have been merged into the main file. You can even nest included files by including a file that contains another #INCLUDE directive.

You can update an included file on your production server without stopping the Web server. However, you cannot include an #INCLUDE in a loop and you cannot recursively include files. For example, file A.inc might have the following line:

<!-- #INCLUDE FILE="B.inc" -->

In this case, you cannot include the following line in file B.inc

<!-- #INCLUDE FILE="A.inc" -->

An attempt to do so will result in an error.

Sharing Script Libraries

In ASP pages, you can share script libraries using the #INCLUDE directive. This allows you to share subroutines and functions between multiple pages. For example, the following file contains a function that returns a string delimited with single quotation marks ('), and a subroutine that takes an array argument and displays the array values in an HTML table:

<!-- shared.inc -->
<%
Function SQLString(cString)
   SQLString = "'" & cString & "'"
End Function
%>

<% 
Sub Array2Table(aArray)
If IsArray(aArray) Then %>
   <TABLE>
   <% For i = 0 to UBound(aArray,1) - 1%>
      <TR>
      <% For j = 0 to UBound(aArray,2) - 1 %>
         <TD> <%= aArray(i,j) %> </TD>
      <% Next %>
      </TR>
   <% Next %>
   </TABLE>
<%End If
End Sub %>

Instead of repeating this script in many of your pages, you can use the following line to make the subroutine and function available:

<!--#INCLUDE FILE="shared.inc" -->

Make sure that the #INCLUDE line comes before any calls to subroutines or functions in the file.

Later, if you want to change some formatting options in the Array2Table subroutine (such as giving the table a border of 1 or a width of 100%), you can make the change in the subroutine in Shared.asp and all your pages will instantly display the table with the new characteristics.

In client script, you can include references to other files in the <SCRIPT> block. Use the SRC attribute to specify the name of a file that contains script you want to include. For example, the following <SCRIPT> block includes a reference to a page containing error message routines.

<SCRIPT SRC="Errmsg.htm"></SCRIPT>

After you have included a reference to a file in this way, you can call script on the page as if it were in the current page.