Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run JavaScript in Windows

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 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?

like image 428
Ne0 Avatar asked Aug 21 '12 15:08

Ne0


People also ask

How do I run a JavaScript file in Windows?

You can Run your JavaScript File from your Terminal only if you have installed NodeJs runtime. If you have Installed it then Simply open the terminal and type “node FileName. js”. If you don't have NodeJs runtime environment then go to NodeJs Runtime Environment Download and Download it.

How do I run JavaScript on my computer?

To execute JavaScript in a browser you have two options — either put it inside a script element anywhere inside an HTML document, or put it inside an external JavaScript file (with a . js extension) and then reference that file inside the HTML document using an empty script element with a src attribute.

How do I run a JavaScript file locally?

Running a JS program from the command line is handled by NodeJS. Start by installing NodeJS on local machine if necessary. Now simply open the command line in the same directory as the index. js script you created (VS Code will do this automatically with the integrated terminal).

How do I open JavaScript on Windows 10?

Click Tools > Internet Options. Click the Security tab > Custom Level. In the Scripting section, click Enable for Active Scripting. In the dialog box that displays, click Yes.


5 Answers

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.

like image 99
Quentin Avatar answered Oct 13 '22 01:10

Quentin


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

like image 43
cringy Avatar answered Oct 13 '22 00:10

cringy


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");
like image 31
mplungjan Avatar answered Oct 13 '22 01:10

mplungjan


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
like image 42
volkinc Avatar answered Oct 13 '22 02:10

volkinc


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

like image 27
Scott Sauyet Avatar answered Oct 13 '22 01:10

Scott Sauyet