Run JavaScript in Windows

前端 未结 5 876
一个人的身影
一个人的身影 2021-02-14 06:21

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.

So for a quick test I crea

相关标签:
5条回答
  • 2021-02-14 06:41

    alert is a method of the browswer's window object. The Window's scripting host does not supply such an object.

    0 讨论(0)
  • 2021-02-14 06:43

    Microsoft's JScript runtime 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.

    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.1 as 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...

    0 讨论(0)
  • 2021-02-14 06:49

    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)

    Since you haven't defined it, you are trying to treat undefined as a function, which it isn't.

    Qnan suggests using the Echo method instead.

    0 讨论(0)
  • 2021-02-14 06:56

    Try a named function replace since WSH does not support the window.alert method.

    if (!alert) alert = function foo(s){WScript.Echo(s)}
    alert("hello world");
    
    0 讨论(0)
  • 2021-02-14 07:01

    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.

    test.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
    
    0 讨论(0)
提交回复
热议问题