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

StackOverflow https://stackoverflow.com/questions/19747522

  •  03-07-2022
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top