从 html 按钮调用 vbscript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16741042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling vbscript function from html button
提问by Baga Jr.
Don't be surprised if what I'm doing is completely wrong or if the solution is obvious.
如果我所做的完全错误或者解决方案很明显,请不要感到惊讶。
<script type="text/vbscript">
Function AddPrinter()
Set objNetwork = CreateObject("WScript.Network")
objNetwork.AddWindowsPrinterConnection "\a2031slhsfile131CAT-T113-HP4014dn"
objNetwork.SetDefaultPrinter "\a2031slhsfile131CAT-T113-HP4014dn"
MsgBox "The printer was added and set as the default printer."
End Function
</script>
I added the above vbscript to an HTML document in the head section and one of the buttons has the following property:
我将上述 vbscript 添加到 head 部分的 HTML 文档中,其中一个按钮具有以下属性:
onclick="AddPrinter()"
I didn't find much about this when searching on Google for an hour. What I did find didn't work. How does it know whether you're calling the function from javascript or vbscript or whatever anyway?
在谷歌上搜索了一个小时,我没有找到太多关于这个的信息。我发现的没有用。它如何知道您是从 javascript 还是 vbscript 或其他任何方式调用该函数?
I get this error:
我收到此错误:
SCRIPT429: ActiveX component can't create object: 'WScript.Network'
回答by Ansgar Wiechers
Your Internet Explorer security settings prevent the creation of the ActiveX control. You need to allow "initialize and script ActiveX controls not marked as safe for scripting".
您的 Internet Explorer 安全设置会阻止创建 ActiveX 控件。您需要允许“初始化和编写未标记为安全脚本的 ActiveX 控件”。
回答by Panayot Karabakalov
Hmm, WScript.Network
works just fine in IE, at least up to version 8.
As about OnClick
, its default assignation (whether run JavaScript
or VBScript
)
depend on script content in the page. If your page has scripts in both
languages (JavaScript
and VBScript
), then JavaScript
is default, i.e.:
嗯,WScript.Network
在 IE 中工作得很好,至少到版本 8。至于OnClick
,其默认分配(无论是运行JavaScript
还是VBScript
)取决于页面中的脚本内容。如果您的页面具有两种语言 (JavaScript
和VBScript
) 的脚本,JavaScript
则为默认值,即:
onClick="MyFunc()" 'is equal to:
onClick="javascript:MyFunc()"
In seach case you'll need to use explicit language modifier to run your VBScript
.
在每种情况下,您都需要使用显式语言修饰符来运行您的VBScript
.
onClick="vbscript:MyFunc()"
But if your page contain only VBScript
then that not necessary.
但是,如果您的页面仅包含VBScript
则没有必要。
<html>
<head>
<title>Example</title>
<script type="text/vbscript">
Sub Hello()
Set objNetwork = CreateObject("WScript.Network")
If IsObject(objNetwork) Then
MsgBox "Its working"
MsgBox "Computer Name = " & objNetwork.ComputerName
Else
MsgBox "Not working"
End If
End Sub
Sub btnTest2_OnClick()
Hello()
End Sub
</script>
</head>
<body>
<button onClick="Hello()">Test 1</button>
<input type="button" value="Test 2" id="btnTest2">
<input type="button" value="Test 3" onClick="Hello()">
</body>
</html>
P.S. And if you not change your IE security settings, as Ansgar Wiechers suggest then you'll need to confirm (on first click).
PS 如果您不更改您的 IE 安全设置,正如 Ansgar Wiechers 所建议的那样,那么您需要确认(在第一次点击时)。
回答by Russell S
You may elect to bypass the security settings of the Internet Explorer completely by saving the file as an HTA rather than an HTML. HTA's are hyper-text applications that are written as htmls and use the Internet Explorer as a GUI, but are not restricted by the same security concerns as an HTML. the only change you need to make is change the file extension from *.html to *.hta.
您可以选择通过将文件保存为 HTA 而不是 HTML 来完全绕过 Internet Explorer 的安全设置。HTA 是编写为 html 并使用 Internet Explorer 作为 GUI 的超文本应用程序,但不受与 HTML 相同的安全问题的限制。您需要做的唯一更改是将文件扩展名从 *.html 更改为 *.hta。