方法一:js中调用WScript.Shell和vbs,因为WScript.Shell在win64位系统中不再支持,
所以最后两个函数在32位系统可以直接运行,64位系统必须调用32位cscript才能运行,具体方法:
开始菜单 -> 运行 -> 输入 "%windir%\SysWoW64\cmd.exe"
->输入 "cscript /path/to/the/test.js"
代码如下,保存为 test.js 即可。
function alert(msg) {
var wss = new ActiveXObject("WScript.Shell");
wss.Popup(msg, 0, "提示", 64);
}
function confirm(msg) {
var wss = new ActiveXObject("WScript.Shell");
return wss.Popup(msg, 0, "确认", 1|32) == 1;
}
function yesNoCancel(msg) {
var shell = new ActiveXObject("WScript.Shell");
return shell.Popup(msg, 0, "确认", 3|32);
}
function prompt(msg) {
var scriptCtrl = new ActiveXObject("ScriptControl");
msg = escape(msg + "");
scriptCtrl.Language = "VBScript";
var sTitle="输入";
var vbsCode = 'InputBox(Unescape("' + msg + '"), "'+sTitle+'")';
//var vbsCode = 'InputBox(Unescape("' + msg + '"), "输入框")';
//alert(vbsCode);
return scriptCtrl.Eval(vbsCode);
}
function inputBox(msg,deflt) {
var scriptCtrl = new ActiveXObject("ScriptControl");
msg = escape(msg + "");
scriptCtrl.Language = "VBScript";
var sTitle="输入";
var vbsCode = 'InputBox(Unescape("' + msg + '"),"'+sTitle+'","'+deflt+'")';
//var vbsCode = 'InputBox(Unescape("' + msg + '"), "输入框")';
//alert(vbsCode);
return scriptCtrl.Eval(vbsCode);
}
alert("hello world");
var x=confirm("ok?");
alert(x);
var x=prompt("ok?");
alert(x);
var x=inputBox("Pls","val");
alert(x);
方法二:混合使用js和VBS,保存为 Test.wsf
<!-- Test.wsf -->
<job id="InputBoxInJS">
<script language="VBScript">
Function VBInputBox(promptText)
VBInputBox = InputBox(promptText,"Input Box")
End Function
</script>
<script language="JavaScript">
function alert(msg) {
var wss = new ActiveXObject("WScript.Shell");
wss.Popup(msg, 0, "提示", 64);
}
function confirm(msg) {
var wss = new ActiveXObject("WScript.Shell");
return wss.Popup(msg, 0, "确认", 1|32) == 1;
}
function echo(x){
var wss = new ActiveXObject("WScript.Shell");
wss.Echo(x);
}
var x = VBInputBox("Enter text")
alert(x);
echo(x);
</script>
</job>
所以最后两个函数在32位系统可以直接运行,64位系统必须调用32位cscript才能运行,具体方法:
开始菜单 -> 运行 -> 输入 "%windir%\SysWoW64\cmd.exe"
->输入 "cscript /path/to/the/test.js"
代码如下,保存为 test.js 即可。
function alert(msg) {
var wss = new ActiveXObject("WScript.Shell");
wss.Popup(msg, 0, "提示", 64);
}
function confirm(msg) {
var wss = new ActiveXObject("WScript.Shell");
return wss.Popup(msg, 0, "确认", 1|32) == 1;
}
function yesNoCancel(msg) {
var shell = new ActiveXObject("WScript.Shell");
return shell.Popup(msg, 0, "确认", 3|32);
}
function prompt(msg) {
var scriptCtrl = new ActiveXObject("ScriptControl");
msg = escape(msg + "");
scriptCtrl.Language = "VBScript";
var sTitle="输入";
var vbsCode = 'InputBox(Unescape("' + msg + '"), "'+sTitle+'")';
//var vbsCode = 'InputBox(Unescape("' + msg + '"), "输入框")';
//alert(vbsCode);
return scriptCtrl.Eval(vbsCode);
}
function inputBox(msg,deflt) {
var scriptCtrl = new ActiveXObject("ScriptControl");
msg = escape(msg + "");
scriptCtrl.Language = "VBScript";
var sTitle="输入";
var vbsCode = 'InputBox(Unescape("' + msg + '"),"'+sTitle+'","'+deflt+'")';
//var vbsCode = 'InputBox(Unescape("' + msg + '"), "输入框")';
//alert(vbsCode);
return scriptCtrl.Eval(vbsCode);
}
alert("hello world");
var x=confirm("ok?");
alert(x);
var x=prompt("ok?");
alert(x);
var x=inputBox("Pls","val");
alert(x);
方法二:混合使用js和VBS,保存为 Test.wsf
<!-- Test.wsf -->
<job id="InputBoxInJS">
<script language="VBScript">
Function VBInputBox(promptText)
VBInputBox = InputBox(promptText,"Input Box")
End Function
</script>
<script language="JavaScript">
function alert(msg) {
var wss = new ActiveXObject("WScript.Shell");
wss.Popup(msg, 0, "提示", 64);
}
function confirm(msg) {
var wss = new ActiveXObject("WScript.Shell");
return wss.Popup(msg, 0, "确认", 1|32) == 1;
}
function echo(x){
var wss = new ActiveXObject("WScript.Shell");
wss.Echo(x);
}
var x = VBInputBox("Enter text")
alert(x);
echo(x);
</script>
</job>