I am currently writing a JavaScript script for the Microsoft JScript Runtime Environment. It's not in a browser, but rather going to be run more like a SysAdmin would use VBScript. I've written a lot of code, and while some of it is specific for what needs to be accomplished, a majority of it is supporting framework for the script to do what it needs to do. I'd like to use this sum of code in other future scripting adventures, but as to my current knowledge, I would have to copy and paste these mini-libraries over and over again, and well, that's just an update nightmare for one and two, it's inefficient. I know it's possible to dynamically load JS when I have a window
or document
, I know it's possible to require()
JS files in Node.js, but is this possible in the raw JScript Runtime for MS?
Asked
Active
Viewed 1,060 times
4

SomeShinyObject
- 7,581
- 6
- 39
- 59
2 Answers
4
Look into the Windows Script File (*.wsf) format. One of its features is to allow for includes like you're describing. An example taken from the linked documentation:
<job id="IncludeExample">
<script language="JScript" src="FSO.JS"/>
<script language="VBScript">
' Get the free space for drive C.
s = GetFreeSpace("c:")
WScript.Echo s
</script>
</job>
where "FSO.JS" contains the JScript library.

Cheran Shunmugavel
- 8,319
- 1
- 33
- 40
-
This...is so full of win. You just saved us a lot of trouble in the future too. – SomeShinyObject Jan 15 '13 at 05:44
0
Assuming your talking about the WSH, you can load a file and eval the contents, which is much the same as including it.
var incfso=new ActiveXObject("Scripting.FileSystemObject");
include = function(x) {
eval(incfso.OpenTextFile(x,1).ReadAll());
}
source: http://www.mailsend-online.com/blog/wsh-javascript-includes.html
I couldn't find a global object like "window" for jscript, but you can sort of create one.
var host = this;
var test = "hello-world";
var messagebox = new ActiveXObject("wscript.shell");
if (host.test) {
messagebox.Popup("host.test exists, value = " + host.test);
} else {
messagebox.Popup("host.test does not exist.");
}
I think "host" should now effectively be the global object. (the example works for me anyway)

NickSlash
- 4,758
- 3
- 21
- 38
-
I have created a `globals` closure to store a majority of the framework. Isn't "eval" evil or is that just in context to the browser? – SomeShinyObject Jan 14 '13 at 15:19
-
If your included files are local, and you made them then its safe yeah, if there remote or someone could modify it without your knowledge then its not safe – NickSlash Jan 14 '13 at 17:29
-
I wanted to like this and I implemented everything correctly and it worked but it slowed everything down drastically because of the incorporation of eval. – SomeShinyObject Jan 15 '13 at 05:46