VBScript 执行方法不声明变量
我正在研究动态“包含”方法,并最终找到了一个使用 VBScript 的执行函数的解决方案。这对我来说非常适合,但我注意到 Execute 执行代码,但此代码无法声明变量或函数之类的内容:
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(Server.MapPath(strFile)) Then
Set objFile = objFSO.OpenTextFile(Server.MapPath(strFile), 1)
strSource = objFile.ReadAll
// Filter out comment- and ASP tags cos they return errors
strSource = Replace(strSource, Chr(60)&Chr(37), "")
strSource = Replace(strSource, Chr(37)&Chr(62), "")
objRegExp.Pattern = "^[ \t]*(//¦\')[\s\S]*?$"
strSource = objRegExp.Replace(strSource, "")
// Execute the code
On Error Resume Next
Execute strSource 'etc........
end if
为什么?谢谢你!
I was looking into dynamic "includes" methods and came down to a solution which uses VBScript's Execute function. This works perfectly for me but I noticed that Execute executes the code but this code cannot declare anything like a variable or function:
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(Server.MapPath(strFile)) Then
Set objFile = objFSO.OpenTextFile(Server.MapPath(strFile), 1)
strSource = objFile.ReadAll
// Filter out comment- and ASP tags cos they return errors
strSource = Replace(strSource, Chr(60)&Chr(37), "")
strSource = Replace(strSource, Chr(37)&Chr(62), "")
objRegExp.Pattern = "^[ \t]*(//¦\')[\s\S]*?$"
strSource = objRegExp.Replace(strSource, "")
// Execute the code
On Error Resume Next
Execute strSource 'etc........
end if
Why? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您想使用
ExecuteGlobal
相反。我想您的动态包含文件加载器位于子例程中,因此当您使用 Execute 时,新变量的范围位于该子例程内。ExecuteGlobal
将确保新变量全局可用。Perhaps you want to use
ExecuteGlobal
instead. I imagine that your dynamic includes file loader is in a subroutine, so when you useExecute
, the new variables are scoped within that subroutine.ExecuteGlobal
will ensure that the new variables are available globally.