views:

119

answers:

5

Hello, I noticed that if I have a .js file in windows explorer (not Internet Explorer, I'm meaning the folder explorer...) I can actually click on it and it will execute, giving error messages, like say "window object is undefined". Is there more information about the environment where the .js script are run into and the objects available?

+7  A: 

Take a look at the Windows Scripting Host Docs (JScript).

Mark B
+1  A: 

The Windows® Scripting Guide provides technical resources, information and source code to help you automate the Windows® operating system using Windows® Script Host (WSH) and the VBScript and JScript scripting languages.

There is much information out there to get you started. There are many things you can do with it. I'm using a VBScript that makes the window handling work as in Linux (alt+Drag moves a window) with jus a few lines of code.

You can access many hooks to the system, including the file system. You can use any language that has registered itself with Windows Script Host, by default, VBScript and JScript.

voyager
A: 

You can run JScript (.js) and VBScript(.vbs) scripts directly in windows.

As what you have is a Javascript file intended to run in a web page, the environment that it expects is different. The window and document objects are only available inside the browser, so they don't work when you run the script outside the browser.

The objects that you have available are instead the ActiveX objects that are registered on the computer, like for example the Scripting.FileSystemObject object that you can use to access the file system.

Guffa
+3  A: 

Windows Script Host provides a reasonably rich environment allowing one to do a variety of interesting things - just yesterday I used it to create a tool that analyses a directory full of XML files which reference various resources such as images and other XML files, and produce an XML manifest in a predefined schema.

It's worth taking the time to get used to creating .wsf files (which use an XML-based syntax), rather than just running .js (JScript) or .vbs (VBScript) files - .wsf files offer much finer control over modularity and allow for better in-file documentation and usage explanations, and also allow scripts written in several different languages to be combined, which is handy if you find a VBScript that does 40% of what you need and don't want the hassle of converting it to use with the 60% you're writing in JScript.

NickFitz
A: 

JavaScript can be executed from the command line of any operating system provided you have access to a JavaScript interpreter that can be executed from the command line. The two common command line JavaScript interpreters are Rhino from Mozilla, which requires Java, and Windows Script Helper which can run natively in a Windows environment.