ASP File Reference
- Previous Page ASP Application
- Next Page ASP Global.asa
The #include directive is used to create reusable functions, headers, footers, or other elements on multiple pages.
The #include directive
By using the #include directive, we can insert another ASP file into this file before the server executes the ASP file. The #include command is used to create reusable functions, headers, footers, or other elements on multiple pages.
How to use the #include directive
There is a file named "mypage.asp":
<html> <body> <h2>Words of Wisdom:</h2> <p><!--#include file="wisdom.inc"--></p> <h2>The time is:</h2> <p><!--#include file="time.inc"--></p> </body> </html>
This is the "wisdom.inc" file:
"One should never increase, beyond what is necessary,</p> the number of entities required to explain anything.
This is the "time.inc" file:
<% Response.Write(Time) %>
The source code viewed in the browser should be similar to this:
<html> <body> <h2>Words of Wisdom:</h2> <p>"One should never increase, beyond what is necessary,</p> the number of entities required to explain anything."</p> <h2>The time is:</h2> <p>11:33:42 AM</p> </body> </html>
The syntax for including files:
To reference a file in ASP, place the #include command within comment tags:
<!--#include virtual="somefilename"-->
Or:
<!--#include file ="somefilename"-->
Keyword Virtual
The keyword 'virtual' indicates that the path starts with the virtual directory.
If the 'header.inc' file is located in the virtual directory '/html', the following line of code will insert the content of the 'header.inc' file:
<!-- #include virtual ="/html/header.inc" -->
Keyword File
The keyword 'File' indicates a relative path. The relative path starts from the directory containing the referencing file.
Assuming the file is located in the subfolder 'headers' of the 'html' folder, the following code can reference the content of the 'header.inc' file:
<!-- #include file ="headers\header.inc" -->
Note:The path of the referenced file 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.
You can also use the keyword 'file' and syntax (..\) to reference files in the parent directory.
Tips and comments
In the previous section, we use ".inc" as the suffix for referenced files. Note: If users try to directly browse INC files, the content of these files will be exposed. If the content of the referenced files involves secrets, it is better to use "asp" as the suffix. The source code in ASP files is compiled and not visible. Referenced files can also reference other files, and a single ASP file can reference the same file multiple times.
Important note:The referenced file is processed and inserted before the script is executed.
The following code cannot be executed because ASP will execute the #include command before assigning values to variables:
<% fname="header.inc" %> <!--#include file="<%=fname%>"-->
File references cannot be included within script delimiters:
<% For i = 1 To n <!--#include file="count.inc"--> Next %>
But this script can work:
<% For i = 1 to n %> <!--#include file="count.inc" --> <% Next %>
- Previous Page ASP Application
- Next Page ASP Global.asa