当前位置:K88软件开发文章中心编程语言AspAsp01 → 文章内容

asp动态include文件的方法

减小字体 增大字体 作者:佚名     来源:asp编程网  发布时间:2018-12-30 8:34:23

在实际工作中,我们在做asp编程开发的时候会偶尔遇到这样的情况:

<
%c=request("c")if c=1 then url="a.asp"else url="b.asp"end if%>
<
!--
#include file="<
%=url%>
"-->
(鼠标移到代码上去,在代码的顶部会出现四个图标,第一个是查看源代码,第二个是复制代码,第三个是打印代码,第四个是帮助)

我们希望能使用上面的方法动态include文件,但是上面的写法是错误的,不能运行。如果真的遇到这样的情况,又希望能够动态include文件如何办?
这里介绍一下一个函数,来实现这样的方法。
原理:使用fso读取include文件的内容,然后去掉<
%和%>
,再把剩下的内容放到当前页面上来,也就是fso读取文件的原因了。具体代码如下:

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=NothingEnd Function
(鼠标移到代码上去,在代码的顶部会出现四个图标,第一个是查看源代码,第二个是复制代码,第三个是打印代码,第四个是帮助)
有了这个函数,上面的代码可以改成
<
%c=request("c")if c=1 then url="a.asp"else url="b.asp"end ifcall include(url)%>
(鼠标移到代码上去,在代码的顶部会出现四个图标,第一个是查看源代码,第二个是复制代码,第三个是打印代码,第四个是帮助)
这样就可以了

asp动态include文件的方法