首页» 沉思录 » asp动态include文件

asp动态include文件

in 沉思录 | 没有评论

受<! #include file="filename.asp" –> 宏限制 
必须存在该文件并且会预先编译(不管前面是否加以条件) ,他总在运行ASP里的程序前先编译,所以无法用if XXX then <! #include file="filename.asp" –>  这种方法随意include文件了

经常有这样的要求,根据不同的需求要求include不同的文件 
如各个人的不同设置,所以要求能动态include文件。 
这里的方法其实原理很简单,就是用FSO把文件读出来,然后 execute运行他,不过这却是很精典的代码了
代码如下: 

 引用内容

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")

关键词:

还没有评论哦,赶快抢沙发~~

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注