16

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?

j08691
209k33 gold badges269 silver badges281 bronze badges
asked Aug 21, 2012 at 15:45
3
  • why are u runing it from the command line instead of putting it in an HTML page? Commented Aug 21, 2012 at 15:47
  • Well, I know enough to know that something you're doing is confusing the parser. I can't say off of the top of my head what though. Commented Aug 21, 2012 at 15:48
  • perhaps because windows command line does not have window.alert available Commented Aug 21, 2012 at 15:49

5 Answers 5

22

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.

answered Aug 21, 2012 at 15:48
Sign up to request clarification or add additional context in comments.

2 Comments

should use something like WSH.Echo("Hello world"); instead
Thanks, i guess i need to read more about it. Thought it may be quicker then writing a java application. I just wanted a scripting language that would allow me to write a quick tool that produced a decent GUI. This is not turning out to be quick!
7

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");
answered Aug 21, 2012 at 15:52

1 Comment

To get this to work I had to give the function a name, e.g. function foo(s){WScript.Echo(s);}
7

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
trlkly
7607 silver badges14 bronze badges
answered Sep 10, 2015 at 15:15

Comments

2

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

answered Aug 21, 2012 at 15:49

Comments

2

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

answered Jan 14, 2016 at 21:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.