views:

2288

answers:

4

I'm stumped and feeling stupid. I've tried every search combination I can think of to figure this out. It seems simple, but being new to javascript I'm not seeing anything helpful in my search for examples or demos. I'm looking to use a script like I would a python or perl script to run a simple linux command. The interpreter is up and running, so I'm just looking for server-side js resources for help in learning more about js. I've found lots of helful examples if I want to do these types of things in a browser - but I don't want to use the browser. I couldn't find a query like this on the site (though I'm sure I'm not asking the right way) so If this is indeed a redundant post feel free to close and point me in the right direction.

A: 

You do not have access to the system beyond the browser. Be it Windows or Linux, your "js shell" is sand boxed to the browser. There is no access to the file system or any applications beyond the browser. JS is a scripting language which is interpreted by the browser.

You have some access to the world outside the browser but only on IE using ActiveX, but then that is outside the sandbox and it not pure javascript anymore. If you search the forums you shall find no documentation that talks about file access in javascript, without ActiveX. ActiveX is not available on Linux or Firefox.

You might want to read up on wikipedia to know more about javascript, DOM and the sandbox. http://en.wikipedia.org/wiki/JavaScript

Varun Mehta
Server-side JavaScript is a different beast, IIRC from the Netscape glory days...
Kev
JavaScript can run out of browsers! See Windows's WSH with its JavaScript as well as VBScript interpreter, both with full system access.
PhiLho
+1  A: 

Look for "system()" on this page, that should be a start (sorry, I haven't actually used SpiderMonkey yet.)

I think if you can compile their JSNative function myjs_system(), you can then call it from within your JavaScript code.

Kev
+1  A: 

Ok, I'm feeling less stupid now. I got it working using Jscript and the jsc.exe included with the .NET Framework on Windows:

> var myFileSystemObj = new ActiveXObject("Scripting.FileSystemObject");
> 
> var pathToFileDir = ".";
> var myFolder = myFileSystemObj.GetFolder(pathToFileDir);
> 
> var myEnum = new
> Enumerator(myFolder.Files);
> 
> for
> (;!myEnum.atEnd();myEnum.moveNext()) {
>   print(myEnum.item()) }

which gives me the file names in a directory nice and easy on the ol' XP. So I guess the question should be is there a similar facility for doing this on Linux? I'm trying to recompile the Spidermonkey engine with the JS_HAS_FILE_OBJECT=1 flag, but it errors out, so I have some reading & work ahead of me to get this working, but I think I'm heading in the right direction. Any hints or suggestions on a different way to do this with javascript would be welcome (beyond "use N", where N = python, perl, java, etc...).

jonny
+1  A: 

For linux I've found that EJScript has file support that works out of the box. It's letting me do what I want to do anyway. I'll still try to get spidermonkey compiled with the File_Object flag because I'm stubborn, but so far I have no problem recommending EJScript for programmers who want to try out serverside javascript.

jonny