How can I create a javascript library in a separate file and "include" it in another?
Asked Answered
L

3

17

First, a caveat. The main script is not run in a webpage. I will be running the .js file in Windows using Windows Script Host.

The problem: I would like to create a javascript "library" containing a number of objects, each with a number of functions. I expect this library will get rather large with time and would like to keep it in a separate javascript file (let's call it Library.js). I would like to access the objects from Library.js from another script (let's call this one User.js).

In essence, I am looking for something similar to the C/C++ "include" paradigm.

Is there anyway to implement this in javascript? (Remember, this is not web-based)

Lazurite answered 29/5, 2009 at 21:28 Comment(1)
Here's a similar question: #316666Grattan
T
21

This page right here is the closest I could find. It talks about .wsf files which let you combine multiple scripts (that may be written in different languages) in one file.

For your question it should be something like:

user.wsf

<job id="IncludeExample">
   <script language="JScript" src="library.js"/>
   <script language="JScript">
      <![CDATA[
      // use your library functions here
      ]]>
   </script>
</job>

There may be better ways, but I'm not a WSH expert.

Transmissible answered 29/5, 2009 at 21:45 Comment(0)
C
15

I know this is an old question and it has been answered and accepted.

I know about .WSF files and how they work; they serve the purpose.

However, I've also found it handy to do the "inclusion" from within a pure .js file that runs in WSH. Here's my solution for that.

    function includeFile (filename) {
        var fso = new ActiveXObject ("Scripting.FileSystemObject");
        var fileStream = fso.openTextFile (filename);
        var fileData = fileStream.readAll();
        fileStream.Close();
        eval(fileData);
    }

With that utility function defined, I can do this from within a javascript module:

includeFile("externalFile1.js");
includeFile("externalFile2.js");
includeFile("etc.js");

...and then I can invoke any of the functions or tickle any of the variables defined in those modules.

A similar technique works with .vbs files:
How do I include a common file in VBScript (similar to C #include)?

Crabbe answered 29/4, 2011 at 17:45 Comment(1)
I tried this includeFile function but unfortunately eval inside this function does not have effect for rest of file. It looks like eval works only inside function and has no global effect. I had to make somethink like: eval(includeFile("something.js")); Where includeFile returns fileData value (return fileData).Gwenni
P
1

You can achieve your goal by using the Windows Scripting Host instead of a specific .js or .vbs file. More information can be found at the MSDN site.

From the link:

Windows script (*.wsf) file is a text document containing Extensible Markup Language (XML) code. It incorporates several features that offer you increased scripting flexibility. Because Windows script files are not engine-specific, they can contain script from any Windows Script compatible scripting engine. They act as a container.

You would cal lthe file using the same syntax as a .js and .vbs file:

wscript.exe myScriptFile.wsf

UPDATED: Correction noted...

Prosecution answered 29/5, 2009 at 21:50 Comment(1)
The script host is either cscript.exe or wscript.exe, not vbscript.exe.Grattan

© 2022 - 2024 — McMap. All rights reserved.