45

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.

4
  • Concat and minify the files on the server and import then at once like <script src="js/toolkit/all.js"></script> Commented Jan 22, 2013 at 13:58
  • You could add only one script which imports dynamically all the scripts. Commented Jan 22, 2013 at 13:58
  • Take a look at this: stackoverflow.com/questions/950087/… Commented Jan 22, 2013 at 14:00
  • This needs to be done server-side. Take a look at this: developers.google.com/closure/compiler Commented Jan 22, 2013 at 14:01

7 Answers 7

19

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

Sign up to request clarification or add additional context in comments.

Comments

12

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.

1 Comment

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.
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.

2 Comments

You can't do this directly, however you can dynamically load script files.
how is a minified JS file supposed to help me when I am in developement mode?
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.

Comments

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.

Comments

1

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

Comments

0

import "./angular.min.js"
import "./jquery-3.6.0.min.js"
import "./app.js"
<script type="module" src="js/importJS.js"></script>

1 Comment

Please read How do I write a good 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.