日期:2010-11-24  浏览次数:20832 次

#include命令用于在多重页面上创建需重复使用的函数、页眉、页脚或者其他元素等。

#include 命令

通过使用#include命令,我们可以在服务器执行某个ASP文件之前,把另一个ASP文件插入这个文件中。#include命令用于在多重页面上创建需重复使用的函数、页眉、页脚或者其他元素等。

如何使用#include命令

这里有一个名为"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> 

这是"wisdom.inc"文件:

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

这是"time.inc"文件:

<%Response.Write(Time)%>

在浏览器中查看的源代码应该类似这样:

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

Including文件的语法:

如需在ASP中引用文件,请把#include命令置于注释标签之中:

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

或者:

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

关键词Virtual

关键词virtual指明位于虚拟目录的路径。

如果名为"header.inc"位于名为/html的虚拟目录中,下面这行代码会插入文件"header.inc"中的内容:

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

关键词File

关键词File可指明一个相对的路径。相对路径起始于含有引用文件的目录。

假设某文件位于html文件夹的子文件夹headers中,下面这段代码可引用"header.inc"文件的内容:

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

注意:被引用文件的路径是相对于引用文件的。假如包含#include声明的文件不在html目录中,这个声明就不会起效。

您同样可以使用关键词file和语法(..\)来引用上级目录中的文件。

提示和注释

在上面的一节中,我们使用".inc"来作为被引用文件的后缀。注意:假如某用户尝试直接浏览某个INC文件,这个文件中内容就会暴露。假如被引用的文件中的内容涉及机密,那么最好还是使用"asp"最为后缀。ASP文件中的源代码被编译后是不可见的。被引用的文件也可引用其他文件,同时ASP文件可以对同一个文件引用多次。

重要事项:在脚本执行前,被引用的文件就会被处理和插入。

下面的代码无法执行,这是由于ASP会在为变量赋值之前执行#include命令:

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

不能在脚本分隔符之间包含文件引用:

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

但是这段脚本可以工作:

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