Latest web development tutorials

ASP references

#include directive

By using the #include directive, you can perform before ASP file on the server, the contents of another ASP file into the ASP file.

#include directive is used to create functions, headers, footers, and other elements need to be repeated or used on several other pages.


How to use the #include directive

There is a file named "mypage.asp" of:

<!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 the 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>


Reference syntax file

For the referenced file in an ASP page, please comment on the #include directive Tags:

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

or

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

Virtual Image

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

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

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

File Keywords

Use the file keyword to indicate a relative path. Relative path is the directory that contains references began.

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

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

Note that the referenced document (headersheader.inc) path is relative to the reference file. If the file containing the #include statement is not in the html directory, the statement will not take effect.


Tips and Notes

In a part of the above, we have used ".inc" is cited as the file extension. Note: If a user tries to browse the INC file, the file contents will be displayed. If your referenced file contains confidential information or any information that you do not want users to see, it is best to use ".asp" extension. After the ASP file in the source code is compiled is not visible. Files can also be cited references other files, while an ASP file can reference the same file multiple times.

IMPORTANT: Before execution of the script, the referenced file will be processed and inserted.The following script can not be executed, because the ASP will be executed before assigning a variable #include directive:

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

You can not file references contained between the script delimiters. Unable to execute the following script:

<%
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 %>