ASP Include Files
#include Directives
By using the #include directive, you can insert the content of another ASP file into an ASP file before the server executes it.
The #include directive is used to create functions, headers, footers, or other elements that need to be reused on multiple pages.
How to Use the #include Directive
Here is a file named "mypage.asp":
Words of Wisdom:
The time is:
This is the "wisdom.inc" file:
"One should never increase, beyond what is necessary,
the number of entities required to explain anything."
This is the "time.inc" file:
<%
Response.Write(Time)
%>
If you view the source code in the browser, it will look like this:
Words of Wisdom:
"One should never increase, beyond what is necessary,
the number of entities required to explain anything."
The time is:
11:33:42 AM
Syntax for Including Files
To include a file in an ASP page, place the #include directive inside a comment tag:
or
The virtual Keyword
Use the keyword virtual to indicate a path that starts with a virtual directory.
If a file named "header.inc" is located in the virtual directory /html, the following line of code inserts the content of the "header.inc" file:
The file Keyword
Use the keyword file to indicate a relative path. A relative path starts with the directory that contains the including file.
If you have a file in the html directory and the "header.inc" file is located at the top of html, the following line of code inserts the content of the "header.inc" file into your file:
Note that the path of the included file (headersheader.inc) is relative to the including file. If the file containing the #include statement is not in the html directory, the statement will not work.
Tips and Notes
In the previous section, we used ".inc" as the file extension for included files. Note that if a user tries to browse an INC file directly, the content of the file will be displayed. If the content of your included file contains confidential information or information you do not want any user to see, it is better to use ".asp" as the extension. The source code in an ASP file is not visible after being compiled. An included file can also include other files, and a single ASP file can include the same file multiple times.
**Important:** Included files are processed and inserted before the script executes. The script below will not work, because ASP executes the #include directive before assigning values to variables:
<%
fname="header.inc"
%>
You cannot include files between script delimiters. The script below will not work:
<%
For i = 1 To n
Next
%>
But this script will work:
<% For i = 1 to n %>
<% Next %>