ASP中利用execute實現(xiàn)動態(tài)包含文件的方法
在ASP中include文件形如 #include file=function.asp,這是最簡單也是最常用的包含文件方法,青島星網(wǎng)下面跟大家分享:根據(jù)不同的需求,包含不同的文件的函數(shù)。
ASP動態(tài)包含文件的實現(xiàn)函數(shù)
Function include(filename) Dim re,content,fso,f,aspStart,aspEnd set fso=CreateObject("Scripting.FileSystemObject") set f=fso.OpenTextFile(server.mappath(filename)) content=f.ReadAll f.close set f=nothing set fso=nothing set re=new RegExp re.pattern="^\s*=" aspEnd=1 aspStart=inStr(aspEnd,content,"<%")+2 do while aspStart>aspEnd+1 Response.write Mid(content,aspEnd,aspStart-aspEnd-2) aspEnd=inStr(aspStart,content,"%\>")+2 Execute(re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write ")) aspStart=inStr(aspEnd,content,"<%")+2 loop Response.write Mid(content,aspEnd) set re=nothing End Function
其實是寫一個動態(tài)包含的函數(shù),這樣每次調(diào)用時候代碼簡潔,也方便,使用方法:
include("***.asp")'注意,這里的include是函數(shù)名哦,不要搞混哦。
下面是其他網(wǎng)友的補充大家參考一下
ASP中,include file/virtual 是優(yōu)先腳本代碼處理的,所以無法使用include動態(tài)包含ASP文件。我們可以使用Execute函數(shù)動態(tài)執(zhí)行所需代碼。
方法:
Execute(ASP代碼)
例子:(vbCrLf為換行符)
Execute("Class clsAbc"&vbCrLf&"Public Function output"&vbCrLf&"Response.Write 123"&vbCrLf&"End Function"&vbCrLf&"End Class")
Dim objAbc Set objAbc = New clsAbc objAbc.output Set objAbc = Nothing
使用時可以用從文件或數(shù)據(jù)庫讀取出ASP代碼再執(zhí)行,注意,所執(zhí)行的代碼中不應(yīng)包含<%和%>
注意不要與Server.Execute混淆,Server.Execute參數(shù)為ASP虛擬路徑,并且使用該函數(shù)不但不能動態(tài)聲明Class類,甚至不可以給主程序段的變量賦值。
例子:
main.asp
Dim strAbc,objAbc strAbc = "Test" Server.Execute("sub.asp") Response.Write strAbc Set objAbc = New clsAbc objAbc.output Set objAbc = Nothing
sub.asp
strAbc = "Execute" Class clsAbc Public Function output Response.Write "Class" End Function End Class
執(zhí)行main.asp后,將僅輸出Test,而objAbc則不能實例化。