ActiveXObject 运行 不 return (shell)

ActiveXObject run does not return (shell)

我正在 运行使用 ActiveXObject 从 JavaScript 环境中创建一个 shell 脚本。如果我不在 运行 函数中包含任何参数,一切都会正常工作:

var x = new ActiveXObject("WScript.Shell");
x.Run("%comspec% /K dir")
alert("I am here!")

根据文档,"The Run method also accepts a Boolean value as a third parameter that determines whether the script pauses until the called program is finished running or instead continues with the next command in the script. If this value is set to False (the default), the Run method simply issues the command to run the program but does not check to ensure that the program actually ran. If the third parameter is set to True, the script will wait for the program to finish running, return the integer exit code provided by the program, and then continue with the next line of the script."

我尝试同时使用两者

x.Run("%comspec% /K dir"), 1, True
alert("I am here!")

x.Run("%comspec% /K dir"), 1, False
alert("I am here!")

但我的代码总是挂起。永远不会显示警报。我究竟做错了什么? (我需要在我的代码中使用第三个参数 = true) 谢谢!

您必须在括号中包含参数:

x.Run("%comspec% /K dir", 1, true);

目前您的脚本使用了几个逗号运算符。首先它执行 Run 方法,计算 1,最后中断到一个未定义的变量名 (True)。 JS区分大小写。