ASP教程:學習掌握ASP 檔案引用的方法

來源:互聯網
上載者:User
教程

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


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.