faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

30 of 47 people (64%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How to send arguments to Command line using Javascript

Apr 7th, 2008 23:17
ha mo, Jean-Bernard Valentaten, James Currie, Colin Fraser, sushil Pan,


I am going to assume you mean the DOS command line in pre- XP Windows or
Linux. 
Firstly, why would you want to? 
Secondly, no it cannot be done. Javascript is designed to work inside
the browser, not outside it. (Someone may actually argue this, but I bet
the argument will be lost on me and probably 99% of other Javascript
users and writers.)  
If you must use something try PHP or Java, javascript was never designed
to be used in the manner you are thinking.
______________________________________________________________________
Firstly? 
First: I can think of many reasons, even in an XP envrionment. Do not 
forget that Javascript WAS designed to be used in exactly this manner. 
JS was not invented by MS and was not inteneded to be a MS specific 
language. In fact it was intended to be platform independent. JS is 
used in Windows through WSH or Windows Scripting Host, and can be 
executed from a command prompt simply by typing the name of the file 
c:\>example.js
Second: Yes it can be done. just like any other executable, any 
paramaters after the file name are passed to the script.
c:\>example.js par1 test
in the case above par1 and test are parameters. In your script access 
this parameter with the following code:
if(WScript.Arguments.count()==2)
{
   var param1 = WScript.Arguments.Item(0);
   var param2 = WScript.Arguments.Item(1);
}
Some good examples of uses:
Writing an XML parser that is passed the name of the file to parse 
using a command line parameter.
Integrating with a 3rd party application that will execute outside 
applications and pass variables through parameters.
______________
Well, now we can all calm down a little and try to focus on the 
question, which was meant as "How do I pass an argument from a 
javascript to the commandline and not vice versa".
I'm afraid Colin is right here. It cannot be done using javascript, 
since it's purpose was (and still is) to access a DOM inside a browser 
(and have limited access to the browser itself).
It is true that Microsoft introduced a WSH-Engine that interpretes a 
scripting language similar to javascript called JScript.
But (!!!) JScript is not javascript. JScript has a lot more access to 
the system than javascript does and it won't run on systems other than 
MS Windows (ok, there are engines for MacOS and a couple of unices *g*)
So, if one wants access to the system, hear my advice: learn to code a 
serverside scripting language (ASP, JSP, PHP ...) in case you wish to 
call external programs on the server or use a Java applet if this has 
to be done clientside.
Well, one might also use JScript, but only if you are absolutely sure 
that you'll only have Windows based clients on your site :)
HTH,
Jean
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com