Function include(filename)
if filename = "" then
exit function
end if
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
注意:如此include 的文件不可以在裏面使用vb和html嵌套的語句。
如: <%IF true Then%>
<p>顯示True</p>
<%Else%>
<p>顯示False</p>
<%End IF%>
此類語句使用該方法會報錯。
建議需要此類的情況請用如下方式替代:
<%
dim html
if true then
html = "顯示True"
else
html = "顯示False"
end if
response.write("<p>" & html & "</p>")
%>
另外一種情況就是嵌套include。可以在每個頁面都使用該函數來include文件。