How to include a JavaScript file in another JavaScript file THAT IS NOT RUN FROM INSIDE A BROWSER? [duplicate]
Asked Answered
S

1

3

I know that many similar questions have been asked before. The difference in my case is that I am using Windows Scripting Host and am running the script from the DOS command line, not from inside a browser.

The only way I can think of doing this is to read the file from the disk as text using fso.OpenTextFile(ThisFile, 1), and then using the eval function to treat the text as code.

Is there a more convenient way?

Smarm answered 2/11, 2013 at 22:18 Comment(2)
Googling for wsh include file javascript seems to turn up good solutions?Fastback
Yes it does seem like a duplicate, sorry!Smarm
R
1

As far as I can tell, no, that's the only real option. Example:

text.js:

var fso = new ActiveXObject("Scripting.FileSystemObject");
var ts = fso.OpenTextFile("foo.js");
var script = ts.ReadAll();
eval(script);
foo();
WScript.Echo(bar);

foo.js:

var bar = "testing";
function foo() {
    WScript.Echo("foo");
}

Output:

foo
testing
Reinold answered 2/11, 2013 at 22:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.