How to reference a JScript file from another one?
Asked Answered
G

3

13

I am writing some server-side scripts using JScript and WSH. The scripts are getting quite long, and some common functions and variables would fit better in a general library script which I included in my various script instances.

But, I cannot find a way reference one JScript file from another. For a moment, I though reading the file contents and passing it to eval() could work. But, as it says on MSDN:

Note that new variables or types defined in the eval statement are not visible to the enclosing program.

Is there any way to include/reference a JScript file from another one?

Genagenappe answered 16/6, 2010 at 14:42 Comment(1)
"Note that new variables or types defined in the eval statement are not visible to the enclosing program." Not as far as I can tell, at least not for WSH: https://mcmap.net/q/904043/-how-to-include-a-javascript-file-in-another-javascript-file-that-is-not-run-from-inside-a-browser-duplicateShoot
M
11

Try using a Windows Script File. It's basically an XML document which allows you to include multiple script files and define multiple jobs, amongst other things.

<!-- MyJob.wsf -->
<job id="IncludeExample">
  <script language="JScript" src="MyLib1.js"/>
  <script language="JScript" src="MyLib2.js"/>
  <script language="JScript">
    WScript.Echo(myLib1.foo());
    WScript.Echo(myLib2.bar());
  </script>
</job>
Monacid answered 17/6, 2010 at 9:31 Comment(3)
Great idea, thank you! But I think I'll stick to my workaround for the time being. It seems less hassle than the WSH fileGenagenappe
Very nice! Now I only have to wait until the box this is running on is upgraded. It's still running on WSH 5.7... :\ This feature has apparently only been implemented in 5.8Defence
I feel it should be noted that this makes debugging without Visual Studio somewhat more complex.Infralapsarian
P
11

Based on Thomas's solution — here's a similar, but more modular approach. First, the script to call:

/* include.js */
(function () {
    var L = {/* library interface */};
    L.hello = function () {return "greetings!";};
    return L;
}).call();

Then, in the calling script:

var Fs = new ActiveXObject("Scripting.FileSystemObject");
var Lib = eval(Fs.OpenTextFile("include.js", 1).ReadAll());
WScript.echo(Lib.hello()); /* greetings! */

Libraries defined this way don't produce or rely on any upvalues, but the eval will return any value it receives from the surrounding anonymous-function in the library.

Puggree answered 24/4, 2012 at 10:1 Comment(1)
This works well. The only downside is that the included library cannot import any system libraries. If the included library requires a system library, that import statement needs to happen in the calling script.Arvonio
G
8

OK, I found a decent solution:

// A object to which library functions can be attached
var library = new Object;
eval((new ActiveXObject("Scripting.FileSystemObject")).OpenTextFile("common_script_logic.js", 1).ReadAll());

// Test use of the library
library.die("Testing library");

I create an object to which I can attach my library functions. That way, I can reach code defined in my library from the calling script. Not perfect, but quite acceptable.

It would be great to see a more proper solution :-)

Genagenappe answered 17/6, 2010 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.