简体   繁体   English

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

[英]How do you pass a javascript variable as an argument to a vbscript function (in the context of HTAs)?

I am writing an HTA and I need to pass a variable that I have in Javascript to a VBScript function. 我正在编写HTA,并且需要将Javascript中的变量传递给VBScript函数。 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? 好处:是否可以从JavaScript代码中触发VBScript函数? 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. 我的HTA相当广泛地使用jQuery,并且能够在jQuery中完成我需要做的系统工作会很高兴。

If a function is defined in VBScript, it can be executed from JavaScript as if it were any other globally available function. 如果在VBScript中定义了一个函数,则可以像其他全局可用函数一样从JavaScript执行该函数。 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: 我曾经使用一个函数,以便可以使用以下代码从我的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>

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. 如果页面上的第一个脚本是vbscript,则它将成为事件处理程序的默认脚本引擎。 If the first is javascript, that would be the default. 如果第一个是javascript,那将是默认值。 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. 提供vbscript:javascript:是一个常见的误解-在JavaScript中,字符串后跟冒号表示通常与循环和break / continue语句配对的标签 In VBScript, it would just cause an error. 在VBScript中,这只会导致错误。 This confusion stems with the method of running script from a URL, eg in the href of an <a> element: 这种混淆源于从URL运行脚本的方法,例如在<a>元素的href中:

<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: 对于您的示例代码,假设closer是全局变量,那么事件处理程序应如下所示:

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

Also, take a look at this MSDN article on using JScript and VBScript on the same page . 另外,请在同一页面上阅读有关使用JScript和VBScript的 MSDN文章。

Your example should work, sure its not doing what you expect because var closer = "C:\\Program Files"; 您的示例应该可以正常工作,请确保它没有达到预期的效果,因为var closer = "C:\\Program Files"; should be var closer = "C:\\\\Program Files"; 应该是var closer = "C:\\\\Program Files"; ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM