top
Loading...
ASP 引用文件

ASP 引用文件


#include 指令

通過使用 #include 指令,您可以在服務器執行 ASP 文件之前,把另一個 ASP 文件的內容插入到這個 ASP 文件中。

#include 指令用於創建函數、頁眉、頁腳或者其他多個頁面上需要重復使用的元素等。


如何使用 #include 指令

這里有一個名為 "mypage.asp" 的文件:

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

這是 "wisdom.inc" 文件:

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

這是 "time.inc" 文件:

<%
Response.Write(Time)
%>

如果您在瀏覽器中查看源代碼,它將如下所示:

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


引用文件的語法

如需在 ASP 頁面中引用文件,請把 #include 指令放在注釋標籤中:

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

or

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

Virtual 關鍵詞

請使用關鍵詞 virtual 來指示以虛擬目錄開始的路徑。

如果一個名為 "header.inc" 的文件位於虛擬目錄 /html 中,下面這行代碼會插入 "header.inc" 文件中的內容:

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

File 關鍵詞

請使用關鍵詞 file 來指示一個相對路徑。相對路徑是以含有引用文件的目錄開始的。

如果您在 html 目錄中有一個文件,且 "header.inc" 文件位於 html 頭部,下面這行代碼將在您的文件中插入 "header.inc" 文件中的內容:

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

請注意被引用文件 (headersheader.inc) 的路徑是相對於引用文件的。如果包含 #include 聲明的文件不在 html 目錄中,這個聲明就不會生傚。


提示和注釋

在上面的一部分中,我們已經使用 ".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 %>

北斗有巢氏 有巢氏北斗