Jim Lawless' Blog


WSH JavaScript Includes

Originally published on: Wed, 28 Apr 2010 00:09:54 +0000

I like to separate JavaScript functions into libraries that I can later reference in some other script.

It's easy enough to reference these in HTML using the SRC attribute of the SCRIPT tag.

I the case of WSH, one can use a WSF document to dynamically construct a composite script via combinations of inline and/or external files. However, I prefer a more simple approach when I'm using WSH.

Please consider these two files:

one.js


   // MIT/X11 license. See includelib.js for full text of
   // license.
eval(new ActiveXObject("Scripting.FileSystemObject").
   OpenTextFile("inc.js",1).ReadAll());

display("Hello!");

inc.js


   // MIT/X11 license. See includelib.js for full text of
   // license.
display=function(x) {
   WScript.Echo(x);
}

The first couple of lines of one.js create a new FileSystemObject. Then, the dot operator is used to immediately refer to that object so that we can invoke OpenTextFile() to open a file called inc.js for mode 1 ( reading ).

Again, the dot operator is used to refer to the new object so that we can call the ReadAll() method to load the entire inc.js file into a string.

Finally, we call eval() to dynamically execute the contents of inc.js, effectively simulating an "include" operation available natively in other scripting languages.

In the inc.js file, I've chosen to use an alternate form of function definition that ensures that the named function will appear globally so that it can be referenced by other included modules.

We prove that we've invoked the included file which defines a function called display() by invoking this new function.

If you execute one.js via the following command-line:

cscript /nologo one.js

...you should see the output: Hello!

If you have a number of files to include, you might not want to keep creating FileSystemObject instances. We can create a library that encapsulates this behavior into a function and can then use the technique from one.js to dynamically evaluate that library.

includelib.js


// This single license text is applied to all examples one,js,
// inc.js, includelib.js, and two.js.
//
// License: MIT / X11
// Copyright (c) 2010 by James K. Lawless
// jimbo@radiks.net http://www.radiks.net/~jimbo
// http://www.mailsend-online.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

   var incfso=new ActiveXObject("Scripting.FileSystemObject");
   include=function(x) {
      eval(incfso.OpenTextFile(x,1).ReadAll());
   }

This library creates one global instance of FileSystemObject, incfso, for future reference.

By dynamically evaluating includelib.js, we now have a callable include() function. We can then include inc.js and can invoke the display method again.

two.js


   // MIT/X11 license. See includelib.js for full text of
   // license.

   // Use the short hack to load
   // includelib.js
eval(new ActiveXObject("Scripting.FileSystemObject").
   OpenTextFile("includelib.js",1).ReadAll());

   // Now invoke include() function to bring in inc.js
include("inc.js");
display("Hello, again!");

When you execute two.js: cscript /nologo two.js

...you should see the text: Hello, again.

Unless otherwise noted, all code and text entries are Copyright 2010 by James K. Lawless


Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer.


Previous post: An Interview with Brad Templeton
Next post:An Interview with the Creator of the BDS C Compiler


About Jim ...