views:

46

answers:

2

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?

A: 

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 :-)

Thomas Svensen
+1  A: 

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>
maerics
Great idea, thank you!But I think I'll stick to my workaround for the time being. It seems less hassle than the WSH file
Thomas Svensen