Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a number of JavaScript modules that contain code that should be broken out into separate modules. However, if I split them up, I must include <script> statements in the HTML files to include them prior to their use. For example, say I have JavaScript code that includes ajax, cookie, font sizing, event handler, and some general purpose utilities. I would like to split these up into:

ajax.js
cookie.js
font_sizing.js
event_handling.js
utilities.js

and then include the files, as necessary, in the JavaScript file. If I break them out but do not have an inclusion mechanism, I am forced to include something like the following in my HTML

<script src="Scripts/ajax.js"></script>
<script src="Scripts/cookie.js"></script>
<script src="Scripts/font_sizing.js"></script>
<script src="Scripts/event_handling.js"></script>
<script src="Scripts/utilities.js"></script>

I would like to have the JavaScript files themselves include them as required. I just don't know a browser-agnostic method to do it. I need vanilla JavaScript only.

Thanks

What I have tried:

Searched Internet for How do I include JavaScript files in an other in a browser-agnostic manner
Posted
Updated 24-Jan-20 10:22am
v2

1 solution

Normally what you would do is to use one file for all of the pages javascripts to reduce the overhead of multiple HTTP calls, or have some other mechanism in place (e.g. #include page, ~bundles), or just do the 5 different script elements.

Doing some searching I found some code on Stack Overflow that may help
Dynamic Script Loading
You could add a script tag with the script URL into the HTML. To avoid the overhead of jQuery, this is an ideal solution.

The script can even reside on a different server. Furthermore, the browser evaluates the code. The {script} tag can be injected into either the web page {head}, or inserted just before the closing {body} tag.
From this I came up with this which is based on the provided code and added in your 5 sample files. You may need to work with getting the URLs to resolve properly, I myself would use fully qualified ones. And naturally you could put this in a completely separate file.
JavaScript
function LoadScript(url) {
     var script = document.createElement("script");
     script.src = url;
     document.head.appendChild(script);
}
LoadScript("Scripts/ajax.js");
LoadScript("Scripts/cookie.js");
LoadScript("Scripts/font_sizing.js");
LoadScript("Scripts/event_handling.js");
LoadScript("Scripts/utilities.js");

Source:
How do I include a JavaScript file in another JavaScript file? - Stack Overflow[^]
 
Share this answer
 
Comments
gggustafson 24-Jan-20 23:12pm    
"You could add a script tag with the script URL into the HTML. To avoid the overhead of jQuery, this is an ideal solution." Unfortunately this transfers the file inclusion out of the file where it is to be included. I like your LoadScript idea. Let me give it some thought. Thanks for your thoughts.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900