#include命令用于在多重页面上创建需重复使用的函数, headers, footers, or other elements.
#include command
By using the #include command, we can insert another ASP file into this file before the server executes an ASP file. #include命令用于在多重页面上创建需重复使用的函数, headers, footers, or other elements.
How to use the #include command
Here is a file called "mypage.asp":
The following are the referenced contents:
<body> <p><!--#include file= "Wisdom.inc"--></p> <p><!--#include file= "Time.inc"--></p> </body>
|
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:
The following are the referenced contents: <% Response.Write (Time) %> |
The source code viewed in the browser should look like this:
The following are the referenced contents:
<body> <p> "One should never increase, beyond what is necessary, The number of entities required to explain anything. </p> <p>11:33:42 am</p> </body>
|
Syntax for including files:
To refer to the file in ASP, place the #include command in the Comment tab:
The following are the referenced contents:
<!--#include virtual= "Somefilename"--> |
Or:
The following are the referenced contents:
<!--#include file = "Somefilename"--> |
Keyword virtual
The keyword virtual indicates the path that is located in the virtual directory.
If the name "Header.inc" is in a virtual directory named/html, the following line of code inserts the contents of the file "Header.inc":
The following are the referenced contents:
<!--#include virtual = "/html/header.inc"--> |
Keyword file
The keyword file can indicate a relative path. The relative path starts with the directory that contains the referenced file.
Suppose a file is in the subfolder headers of an HTML folder, and the following code refers to the contents of the "Header.inc" file:
The following are the referenced contents:
<!--#include file = "Headers\header.inc"--> |
Note: The path to the referenced file is relative to the referencing file. This declaration is not valid if the file containing the #include declaration is not in the HTML directory.
You can also use the keyword file and syntax (... \) to refer to files in the parent directory.
Tips and comments
In the above section, we use ". Inc" as the suffix of the referenced file. Note: If a user tries to navigate directly to an inc file, the contents of the file are exposed. If the content in the referenced file involves confidentiality, it is best to use the "ASP" suffix. The source code in the ASP file is not visible when it is compiled. Referenced files can also refer to other files, and ASP files can refer to the same file multiple times.
Important: The referenced file is processed and inserted before the script executes.
The following code cannot be executed because ASP executes the #include command before assigning a value to a variable:
The following are the referenced contents:
<% Fname= "Header.inc" %> <!--#include file= "<%=fname%>"--> |
You cannot include a file reference between script delimiters:
The following are the referenced contents: <% For i = 1 to n <!--#include file= "Count.inc"--> Next %> |
But this script can work:
The following are the referenced contents:
<% for i = 1 to n%> <!--#include file= "Count.inc"--> <% Next%> |