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. Views expressed in the comments are those of the responding individual.

stumbleupon Save to StumbleUpon
digg Digg it
reddit Save to Reddit
facebook Share on Facebook
twitter Share on Twitter
aolfav More bookmarks


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


About Jim ...


Follow me on Twitter

http://twitter.com/lawlessGuy


My GitHub Repository

https://github.com/jimlawless


Recent Posts

Compiling C from the Command Line with Pelles C

A Forthcoming Marvel Movie Villain

Uninstalling Problematic Windows Software

Don't be Hatin'

A JavaScript REPL for Android Devices

MailSend is Free

My Blog Engine

The October 10th Bug


Random Posts

Changing the C64 Text Color in C

Blogoversary

A Forthcoming Marvel Movie Villain

Obfuscated Perl

Speeding up JRuby with NailGun

Invoking the Default Windows Screen-Saver

A Lightweight Alternative to Windows Shortcuts

BSave and BLoad for the Commodore 64

Envy

A DSL in JavaScript


Full List of Posts

http://www.mailsend-online.com/bloglist.htm


Recent Posts from my Other Blog / GitHub

A Simple (but useful) TCP Client Written in Go

Yet Another Seven Languages in Seven Weeks

Happy 50th Birthday, BASIC

Extending Commodore 64 BASIC

Hide the HTTP-Referer using HTML and JS

RSS Feed Processing in Python

A Chromecast Slideshow using Python

A Simple Perl REPL

Linux Mint on a Toshiba Netbook

Find and View All Images on a HD with Perl

Pretty-Printing an s-expression in Go

My Personal Text to HTML Utility

1985 Computing : Atari and Commodore

My Mac has Mono

Yet Another Config File Reader for Go (Golang)

Filling a Slice Using Command-line Flags in Go (Golang)

An RPN Interpreter in Go (Golang)

Simulating Try-Catch in Go (Golang)

Sending GMail with Go (Golang)

Variant Types in Golang

My First C64 Interrupt Program

The Triangles Puzzle

Happy 25th, Perl !

My Favorite BASIC One-Liner

Playing with OS/X Text to Speech

The Villain at the end of Marvel's Avengers Move is...

Chicken! Fight like a Robot!

Processing GMail items with Ruby

The Squares Puzzle

Happy 30th Birthday, Commodore 64

Scripting Safari

MailWrench CSharp Command Line Mailer for Windows is now Free Software

Welcome Back, M.U.L.E. !

Rainy Day Fun with the HTML DOM

Building a World War II Foxhole Radio

Prototerp Unleashing a JavaScript REPL via the Mobile Browser

Steal This Bookmarklet

Happy Birthday, Miles