views:

518

answers:

3

Does anyone know how to set the working directory in JavaScript before?

Code I use for launching an application is this:

// Create an object script
oL = new ActiveXObject("WScript.Shell");
oFile = '"C:/Application.exe"';
oL.run(oFile);
A: 

Javascript dosent have access to your harddrive so why should you be able to set the working directory?

Petoj
@Petoj: While it is true JavaScript in a web page loaded in a web browser typically does not have access to your local file resources, JavaScript *can* access your local file resources when running in a web browser if the user has provided the appropriate permissions, or if the script is run through Windows Script Host, or when it is executing as part of a Firefox Extension.
Grant Wagner
+1  A: 

Javascript typically runs in a sandbox that means it doesn't have access to the filesystem anyway, thus setting the cwd is meaningless.

What context are you trying to do this in (website javascript, local script running with Rhino etc.) and what are you trying to achieve?

Andrzej Doyle
+5  A: 

According to MSDN, you should be able to use:

var oL = new ActiveXObject("WScript.Shell");
oL.CurrentDirectory = "C:\\Foo\\Bar";
oFile = '"C:\\Application.exe"';
oL.run(oFile);

...assuming you're running this script in Windows Script Host, in which case you probably ought to make that clear in your question - about 99% of JavaScript programmers only ever use the language in a web browser, where this kind of stuff is only possible under extremely unusual circumstances.

NickFitz