主题:[讨论]如何在ASP中动态包含文件?(VBScript)
我想在ASP中根据不同的计算结果包含不同的文件,直接用<!--#include file=" " --> 是不行的,因为<!--#include file=""--> 会先于其他任何代码编译,我在网上搜到一个函数
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
用include("inc.asp")的方法调用该函数
但我在使用过程中遇到一个问题:当被包含文件中不含<%和%>时可以正常调用,但当被包含文件中包含<%和%>时会报错,出错语句是Execute(re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write ")),提示 无效的过程调用或参数: 'Mid'
麻烦帮忙改改,谢谢!
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
用include("inc.asp")的方法调用该函数
但我在使用过程中遇到一个问题:当被包含文件中不含<%和%>时可以正常调用,但当被包含文件中包含<%和%>时会报错,出错语句是Execute(re.replace(Mid(content,aspStart,aspEnd-aspStart-2),"Response.Write ")),提示 无效的过程调用或参数: 'Mid'
麻烦帮忙改改,谢谢!