如何将javascript变量作为参数传递给vbscript函数(在HTA的上下文中)? [英] How do you pass a javascript variable as an argument to a vbscript function (in the context of HTAs)?

查看:108
本文介绍了如何将javascript变量作为参数传递给vbscript函数(在HTA的上下文中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写HTA,我需要将Javascript中的变量传递给VBScript函数。你能告诉我怎么做吗?这是我正在尝试做的一个(非工作)示例:

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.

奖励:是否有可能从javascript中激活VBScript函数?我的HTA非常广泛地使用jQuery,能够在jQuery中完成我需要做的系统工作会很好。

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.

推荐答案

如果在VBScript中定义了一个函数,它可以从JavaScript执行,就好像它是任何其他全局可用的函数一样。两种脚本语言都共享全局变量和函数。我以前使用一个函数,以便我可以使用以下代码从我的JavaScript代码访问MsgBox:

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:

<script type="text/vbscript">
Function vbsMsgBox (prompt, buttons, title)
    vbsMsgBox = MsgBox(prompt, buttons, title)
End Function
</script>
<script type="text/javascript">
vbsMsgBox("This is a test", 16, "Test");
</script>

混合使用这些脚本时,包含顺序非常重要。如果页面上的第一个脚本是vbscript,那么它将成为事件处理程序的默认脚本引擎。如果第一个是javascript,那将是默认值。提供 vbscript: javascript:是一种常见的误解 - 在JavaScript中,后跟冒号的字符串表示 label 通常与循环和break / continue语句配对。在VBScript中,它只会导致错误。这种混淆源于从URL运行脚本的方法,例如在< a> 元素的href中:

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: or javascript: 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:

<a href="javascript:doSomething(); void(0);">do something</a>

使用示例代码,假设 close 是一个全局变量然后您的事件处理程序应如下所示:

With your sample code, assuming closer is a global variable then your event handler should look like this:

<a href="#" onclick="CloseExplorerWindow(closer)">close</a>

另外,看看这篇关于使用同一页面上的JScript和VBScript

Also, take a look at this MSDN article on using JScript and VBScript on the same page.

这篇关于如何将javascript变量作为参数传递给vbscript函数(在HTA的上下文中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆