如何在ASP页面动态Inclue文件?
程序员文章站
2022-06-19 23:54:00
受 宏限制 必须存在该文件并且会预先编...
受<! #include file="filename.asp" --> 宏限制
必须存在该文件并且会预先编译(不管前面是否加以条件)
经常有这样的要求,根据不同的需求要求include不同的文件
如各个人的不同设置,所以要求能动态include文件。
代码如下:
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("youinc.asp")
必须存在该文件并且会预先编译(不管前面是否加以条件)
经常有这样的要求,根据不同的需求要求include不同的文件
如各个人的不同设置,所以要求能动态include文件。
代码如下:
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("youinc.asp")