ASP #include



#include directive

By using the #include directive, you can insert the contents of another ASP file into this ASP file before the server executes the ASP file. 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":

<!DOCTYPE html>
<html>
<body>
<h3>Words of Wisdom:</h3>
<p><!--#include file="wisdom.inc"- -></p>
<h3>The time is:</h3>
<p><!--#include file="time.inc"-->< ;/p>
</body>
</html>

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 a browser, it will look like this:

<!DOCTYPE html>
<html>
<body>
<h3>Words of Wisdom:</h3>
<p>"One should never increase, beyond what is necessary,
the number of entities required to explain anything."</p>
<h3>The time is:</h3>
<p>11:33:42 AM</p>
</body>
</html>


##Syntax for referencing files

If you need to use ASP To reference files in the page, please put the #include directive in the comment tag:

<!--#include virtual="somefilename"-->

or

<!--#include file ="somefilename"-->
Virtual keyword

Please use the keyword virtual to indicate that the directory starts with a virtual directory path.

If a file named "header.inc" is located in the virtual directory /html, the following line of code will insert the contents of the "header.inc" file:

< ;!-- #include virtual ="/html/header.inc" -->
File keyword

Please use the keyword file to indicate a relative path. Relative paths start with the directory containing the referenced file.

If you have a file in the html directory, and the "header.inc" file is located in the html header, the following line of code will insert the contents of the "header.inc" file into your file:

<!-- #include file ="headersheader.inc" -->

Please note that the path of the referenced file (headersheader.inc) is relative to the referencing file. If the file containing the #include statement is not in the html directory, this statement will not take effect.


Tips and Notes

In the above section, we have used ".inc" as the file extension for the referenced file. Please note: If the user attempts to browse the INC file directly, the contents of this file will be displayed. If the content in your referenced file contains confidential information or information that 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 compiled and invisible. Referenced files can also reference other files, and an ASP file can reference the same file multiple times.

Important: The referenced file will be processed and inserted before the script is executed. The following script cannot be executed because ASP executes the #include directive before assigning a value to the variable:

<%
fname="header.inc"
%>
<!--#include file="<%fname%>"-->

You cannot include file references between script delimiters. The following script cannot be executed:

<%
For i = 1 To n
<!--#include file="count.inc"-->
Next
%>

But this script can be executed:

<% For i = 1 to n %>
< !--#include file="count.inc" -->
<% Next %>