如何将 javascript 变量作为参数传递给 vbscript 函数(在 HTA 的上下文中)?

发布于 2024-09-11 17:46:17 字数 739 浏览 5 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄凉少年不暖心 2024-09-18 17:46:17

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

<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 中,后跟冒号的字符串表示 标签 通常与循环和 Break/Continue 语句配对。在 VBScript 中,它只会导致错误。这种混乱源于从 URL 运行脚本的方法,例如在 元素的 href 中:

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

使用示例代码,假设 closer 是全局变量那么你的事件处理程序应该如下所示:

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

另外,请查看这篇有关使用 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:

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

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>

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>

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

深爱不及久伴 2024-09-18 17:46:17

您的示例应该可以工作,确保它没有执行您期望的操作,因为 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"; ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文