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

喜你入骨 提交于 2019-12-02 00:56:54

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.

Your example should work, sure its not doing what you expect because var closer = "C:\Program Files"; should be var closer = "C:\\Program Files"; ?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!