如何将 javascript 变量作为参数传递给 vbscript 函数(在 HTA 的上下文中)?
我正在编写一个 HTA,我需要将 Javascript 中的变量传递给 VBScript 函数。你能让我知道该怎么做吗?这是我想要做的一个(不起作用的)示例:
<!DOCTYPE ... >
<html>
<head>
<HTA:APPLICATION ID="chrome" APPLICATIONNAME="kiosk" ... />
...
<script type="text/javascript">
...
var closer = "C:\Program Files";
...
</script>
<script language="VBScript" src="close.vbs"></script>
</head>
<body>
<a href="#" onClick="VBScript:CloseExplorerWindow(window.closer)">close</a>
</body>
</html>
请记住,这个示例过于简单化了 - 我只是试图消除所有复杂性并向您展示我实际上正在尝试的内容去做。
额外奖励:是否可以从 javascript 函数触发 VBScript 函数?我的 HTA 广泛使用 jQuery,如果能够在 jQuery 中完成我需要做的系统工作,那就太好了。
I am writing an HTA and I need to pass a variable that I have in Javascript to a VBScript function. Can you please let me know how to do this? Here is a (nonworking) example of what I'm trying to do:
<!DOCTYPE ... >
<html>
<head>
<HTA:APPLICATION ID="chrome" APPLICATIONNAME="kiosk" ... />
...
<script type="text/javascript">
...
var closer = "C:\Program Files";
...
</script>
<script language="VBScript" src="close.vbs"></script>
</head>
<body>
<a href="#" onClick="VBScript:CloseExplorerWindow(window.closer)">close</a>
</body>
</html>
Please bear in mind that this example is waaaay oversimplified - I've just tried to strip out all the complexity and present you with what it is I'm actually trying to do.
Bonus: Is is possible to fire a VBScript function from a javascript one? My HTA uses jQuery quite extensively and it'd be nice to be able to do the system stuff I need to do from within jQuery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果函数是在 VBScript 中定义的,则可以从 JavaScript 执行它,就像它是任何其他全局可用函数一样。两种脚本语言共享全局变量和函数。我曾经使用一个函数,以便可以使用以下命令从 JavaScript 代码访问 MsgBox:
混合这些脚本时,包含的顺序很重要。如果页面上的第一个脚本是 vbscript,它将成为事件处理程序的默认脚本引擎。如果第一个是 javascript,那么这将是默认值。提供
vbscript:
或javascript:
是一种常见的误解 - 在 JavaScript 中,后跟冒号的字符串表示 标签 通常与循环和 Break/Continue 语句配对。在 VBScript 中,它只会导致错误。这种混乱源于从 URL 运行脚本的方法,例如在元素的 href 中:
使用示例代码,假设
closer
是全局变量那么你的事件处理程序应该如下所示:另外,请查看这篇有关使用 JScript 和 VBScript 在同一页面上。
If a function is defined in VBScript, it can be executed from JavaScript as if it were any other globally available function. Both scripting languages share global variables and functions. I used to use a function so that I could access MsgBox from my JavaScript code using the following:
The order of inclusion is important when mixing these scripts. If the first script on your page is vbscript, that becomes the default scripting engine for event handlers. If the first is javascript, that would be the default. Providing
vbscript:
orjavascript:
is a common misconception - in JavaScript a string followed by a colon indicates a label commonly paired with loops and break/continue statements. In VBScript, it would just cause an error. This confusion stems with the method of running script from a URL, e.g. in the href of an<a>
element:With your sample code, assuming
closer
is a global variable then your event handler should look like this:Also, take a look at this MSDN article on using JScript and VBScript on the same page.
您的示例应该可以工作,确保它没有执行您期望的操作,因为
var close = "C:\Program Files";
应该是
var close = "C:\\Program Files";
?Your example should work, sure its not doing what you expect because
var closer = "C:\Program Files";
should be
var closer = "C:\\Program Files";
?