42

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.

Armand
  • 23,463
  • 20
  • 90
  • 119
Oliver Watkins
  • 12,575
  • 33
  • 119
  • 225

7 Answers7

18

Including a JavaScript file in another JavaScript file is common in web development.

Because many times we include the JavaScript file at run time on the behalf of some conditions.

So, we can achieve this using JavaScript as well as using jQuery.

Method 1: Use JavaScript to include another JavaScript file

  1. Add the following function in your web page.

    function loadScript(url)
    {    
        var head = document.getElementsByTagName('head')[0];
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = url;
        head.appendChild(script);
    }
    
  2. just call the below loadScript function, where you want to include the js file.

    loadScript('/js/jquery-1.7.min.js');
    

Method 2: Use jQuery to include another JavaScript file.

  1. Add jQuery File in your webpage.

    <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
    
  2. Just call the getScript function functions.

    jQuery(document).ready(function(){
        $.getScript('/js/jquery-1.7.min.js');
    });
    

How to include a JavaScript file in another JavaScript File

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
13

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.

Binary Brain
  • 1,170
  • 8
  • 20
  • 1
    I think this is the best answer. It may increase load time, but if a website takes 300ms to load instead of 3ms then its not a big deal. – Oliver Watkins Jul 22 '15 at 06:55
7

No you can't do it. And by the way this is not good idea to load all 50 separate files. Consider compressing them in one single script to improve performance and decrease page load time.

dfsq
  • 191,768
  • 25
  • 236
  • 258
5

You can setup grunt to watch the folder of the scripts and concat/minify them into a single file, then you just have to include that in your HTML file.

thSoft
  • 21,755
  • 5
  • 88
  • 103
2

You will need to specify each file for the browser to know what to retrieve, but depending on the IDE you are using, there may be shortcuts for doing this, (Visual Studios allows you to drag and drop script files into the html to add references).

During development, you want to do just as your doing by keeping the files separate for troubleshooting, but in production, as others have commented, its very good practice to minify your code and combine them into one file. That makes it only one call, and can reduce your overhead significantly.

Ben Felda
  • 1,474
  • 10
  • 15
1

I recommend you to use JSCompress. It will compress all your javascript code into one single javascript file.

Orlandster
  • 4,706
  • 2
  • 30
  • 45
-1

import "./angular.min.js"
import "./jquery-3.6.0.min.js"
import "./app.js"
<script type="module" src="js/importJS.js"></script>
  • 3
    Please read [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). While this code block may answer the OP's question, this answer would be much more useful if you explain how this code is different from the code in the question, what you've changed, why you've changed it and why that solves the problem without introducing others. – Saeed Zhiany Jul 27 '22 at 02:52