Getting Started with ASP (17)-asp #include

Source: Internet
Author: User

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

How to use #include directives

There is a file named mypage.asp :

<%@LANGUAGE="VBSCRIPT"CODEPAGE="65001"%><!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><title>How to use #include directives</title></Head><Body><H3>Wisdom</H3><!--the Wisdom.inc file is referenced here -<P><!--#include file= "Wisdom.inc" -</P><H3>Now the time is:</H3><!--This applies the time.asp file to show the current time -<P><!--#include file= "time.asp" -</P></Body></HTML>

This is the wisdom.inc file content:

"Good code is its own best document. When you're thinking about adding a comment, ask yourself, "How can you improve this code so that it doesn't need annotations?" "--steve McConnell" Code Encyclopedia "
This is time.aspFile contents:
<% Response.Write (time) %>

After you run the mypage.asp file in your Firefox browser, the following results appear:

With view page source , it looks like this:

Syntax for introducing files

To reference a file in an ASP page, place the #include directive in the Comments tab <!---->:

<!----or<!-- --

The file followed #include virtual is the "absolute path" starting with the dummy directory.

#include file is followed by a relative path to the files, and the absolute path cannot be used, for example:

We learn the difference by example, the files that need to be called (time.asp and Wisdom.inc) are stored in the inc directory, and the main page (mypage.asp) is stored in the test directory, as shown in the tree structure:

Our mypage.asp are quoted in two different ways, the main code is as follows:

......<Body><H1>A reference to #include file relative address method demo</H1><H3>Wisdom</H3><P><!--#include file= ". /inc/wisdom.inc " -</P><H3>Now the time is:</H3><P><!--#include file= ". /inc/time.asp " -</P><HR><H1>Demo of #include virtual absolute address mode</H1><H3>Wisdom</H3><P><!--#include virtual= "/inc/wisdom.inc" -</P><H3>Now the time is:</H3><P><!--#include virtual= "/inc/time.asp" -</P></Body>......

After the call, the following results are displayed:

Special Tips

Through the above section, we have used the file name extension of INC as the referenced file. If a user tries to browse the INC file directly, the contents of this file will be displayed. It is best to use ASP as an extension.

The referenced files are processed and inserted before the script executes. The following script does not work because ASP executes the #include directive before assigning a value to a variable, as follows:

<%="/inc/wisdom.inc"%><!--    -

After the operation will be an error, prompted not to find the included file <%fname%>.

You cannot include a file reference between the script delimiter <%%>, and you cannot execute it as follows:

<% forI= 1  to Ten<!--#include virtual="/inc/wisdom.inc" -Next%>

But the following code can be executed, referenced 10 times, as follows:

<%  for  =1to%><!--#include virtual= "/inc/ Wisdom.inc "-<%Next%>

The referenced file can reference other files, and the same ASP file can refer to the same file multiple times.

Sample code Download

Includetest.rar

Getting Started with ASP (17)-asp #include

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.