在 Windows 中运行 JavaScript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12058548/
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
Run JavaScript in Windows
提问by Ne0
I thought for some simple tests that just run a few commands i would try using some JavaScript and run it from the command line in Windows XP.
我想对于一些只运行一些命令的简单测试,我会尝试使用一些 JavaScript 并从 Windows XP 的命令行运行它。
So for a quick test I created a script
所以为了快速测试,我创建了一个脚本
alert('Hello, World!');
Then tried to run it
然后尝试运行它
D:\>Cscript.exe hello.js
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.
D:\hello.js(1, 1) Microsoft JScript runtime error: Object expected
Google has not helped and I am sure I am missing something silly, can any of you guys shed any light on why this simple script doesn't run?
谷歌没有帮助,我确定我错过了一些愚蠢的东西,你们中的任何人都可以解释为什么这个简单的脚本不能运行吗?
回答by Quentin
You are calling a function called alert
, but this is not part of JavaScript (it is part of DOM 0 and is provided by browsers)
您正在调用一个名为 的函数alert
,但这不是 JavaScript 的一部分(它是 DOM 0 的一部分,由浏览器提供)
Since you haven't defined it, you are trying to treat undefined
as a function, which it isn't.
由于您尚未定义它,因此您正试图将其undefined
视为一个函数,而事实并非如此。
Qnan suggests using the Echomethod instead.
Qnan 建议改用Echo方法。
回答by mplungjan
Try a named function replace since WSH does not support the window.alert method.
尝试命名函数替换,因为 WSH 不支持 window.alert 方法。
if (!alert) alert = function foo(s){WScript.Echo(s)}
alert("hello world");
回答by volkinc
A good approach is to redirect all of the usual output like in a following examples. It will allow you to test JavaScript designed for web without needing to rewrite.
一个好的方法是重定向所有常用输出,如以下示例中所示。它将允许您测试专为 Web 设计的 JavaScript,而无需重写。
test.js
测试.js
var console = {
info: function (s){
WSH.Echo(s);
}
}
var document = {
write : function (s){
WSH.Echo(s);
}
}
var alert = function (s){
WSH.Echo(s);
}
console.info("test");
document.write("test2");
alert("test3");
You can call the script like this:
您可以像这样调用脚本:
Cscript.exe test.js firstParam secondParam
which will give you:
这会给你:
test
test1
test2
回答by Scott Sauyet
alert
is a method of the browswer's window
object. The Window's scripting host does not supply such an object.
alert
是浏览器window
对象的方法。Window 的脚本宿主不提供这样的对象。
回答by cringy
Microsoft's JScriptruntime compiler does not provide the native JavaScript popups as found in the DOM (Document Object Model) which is supported by all major browsers today. However, this can be done by wrapping a function (in your case alert
) around the native MessageBox
found in WSH (Windows Scripting Host) as with any other scripting language supported with WSH.
Microsoft 的JScript运行时编译器不提供当今所有主要浏览器都支持的 DOM(文档对象模型)中的原生 JavaScript 弹出窗口。但是,这可以通过在 WSH(Windows 脚本主机)中找到alert
的本机周围包装一个函数(在您的情况下)来完成,MessageBox
就像 WSH 支持的任何其他脚本语言一样。
But, just to give you an easier option... try DeskJS. It's a new console-style app for Windows that's designed to run pure JavaScript (ECMAScript 5.1as of currently) away from the browser and supports all the basic JavaScript popup boxes together with other nifty additions to the language. You may just love it more than the browser's console...
但是,只是为了给你一个更简单的选择......试试DeskJS。这是一款适用于 Windows 的全新控制台式应用程序,旨在远离浏览器运行纯 JavaScript(目前为ECMAScript 5.1),并支持所有基本的 JavaScript 弹出框以及该语言的其他精美补充。你可能只是喜欢它而不是浏览器的控制台......