Oliver Watkins Oliver Watkins - 7 months ago 22
HTML Question

How do you import multiple javascript files in HTML index file without the bloat?

Is there anyway of importing multiple javascript files in HTML without having to specify each file?

<script src="js/toolkit/Toolkit.js"></script>
<script src="js/toolkit/Viewable.js"></script>
<script src="js/toolkit/Overlay.js"></script>


ie. can I specify something like js/toolkit/* ?

I have 50+ javascript files that i have to import, and to specify each file seems very time consuming.

Answer

There's a way:

You can create a javascript function that takes the path as a parameter and creates these HTML lines:

<script src="js/toolkit/Toolkit.js"></script>
<script src="js/toolkit/Viewable.js"></script>
<script src="js/toolkit/Overlay.js"></script>

And you'll just have to call this:

loadLib("toolkit/Toolkit");
loadLib("toolkit/Viewable");
loadLib("toolkit/Overlay");

But this is not recommended because the load time will be increased due to the number of HTTP requests. You should better use something in the server side to put everything in the same file.