HOME

ASP Tutorial
ASP HOME
ASP Introduction
ASP Install
ASP Syntax
ASP Variables
ASP Procedures
ASP Forms
ASP Cookies
ASP Session
ASP Application
ASP #include
ASP Global.asa

ASP Objects
ASP Response
ASP Request
ASP Application
ASP Session
ASP Server
ASP Error

ASP FileSystem
ASP TextStream
ASP Drive
ASP File
ASP Folder
ASP Dictionary

ASP Components
ASP AdRotator
ASP BrowserCap
ASP Content Linking
ASP Content Rotator

ASP Quick Ref

Examples/Quiz
ASP Examples
ASP Quiz Test

ADO
ADO Introduction

Resources
ASP Resources
Proposal Kit
Please visit our sponsors !

ASP Including Files

previous next

The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages.


The #include Directive

It is possible to insert the content of another file into an ASP file before the server executes it, with the #include directive. The #include directive is used to create functions, headers, footers, or elements that will be reused on multiple pages.


How to Use the #include Directive

Here is a file called "mypage.asp":

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

Here is the "wisdom.inc" file:

"One should never increase, beyond what is necessary,
the number of entities required to explain anything."

Here is the "time.inc" file:

<%
Response.Write(Time)
%>

If you look at the source code in a browser, it will look something like this:

<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 Including Files

To include a file into an ASP page, place the #include directive inside comment tags:

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

Using the Virtual Keyword

Use the virtual keyword to indicate a path beginning with a virtual directory.

For example, if a file named header.inc resides in a virtual directory named /html, the following line would insert the contents of header.inc into the file containing the line:

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

Using the File Keyword

Use the file keyword to indicate a relative path. A relative path begins with the directory that contains the including file.

For example, if you have a file in the directory html, and the file header.inc is in html\headers, the following line would insert header.inc in your file:

<!-- #include file ="headers\header.inc" -->

Note that the path to the included file, headers\header.inc, is relative to the including file; if the script containing this #include statement is not in the directory /html, the statement would not work.

You can also use the file keyword with the syntax (..\) to include a file from a higher-level directory.


Tips and Notes

In the sections above we have used the file extension ".inc" for the included files. Notice that if a user tries to browse a ".inc" file directly, its content will be displayed. So if your included file contains source code you do not want any users to see, it is better to use a ".asp" extension. The source code in a ".asp" file will not be visible after the interpretation.

An included file can include other files, and one ASP file can include the same file more than once.

Important: Included files are processed and inserted before the scripts are executed.

The following script will not work because ASP executes the #include directive before it assigns a value to the variable:

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

You can not open or close a script delimiter in a ".inc" file. This script will not work:

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

But this script will work:

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


previous next

Jump to: Top of Page or HOME or Printer Friendly Printer friendly page


Search W3Schools:


What Others Say About Us

Does the world know about us? Check out these places:

Dogpile Alta Vista MSN Google Excite Lycos Yahoo Ask Jeeves


We Help You For Free. You Can Help Us!


W3Schools is for training only. We do not warrant its correctness or its fitness for use. The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2002 by Refsnes Data. All Rights Reserved


Validate How we converted to XHTML Validate

ASPHits
Active Server Pages Resource Web Site


Your own Web Site?

Read W3Schools
Hosting Tutorial



$15 Domain Name
Registration
Save $20 / year!



Advertise
at W3Schools

Only 0.5 CPM



SELECTED LINKS

University Online
Master Degree
Bachelor Degree


Web Software

The Future of
Web Development


Jobs and Careers

Web Security
Web Statistics
Web Standards


PARTNERS

W3Schools
TopXML
VisualBuilder
XMLPitstop
DevelopersDex
DevGuru
Programmers Heaven
The Code Project
Tek Tips Forum
ZVON.ORG
TopXML Search