How To Import Multiple JavaScript Files

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.

How to Include a JavaScript file in another JavaScript file?

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

Method 1: Use JavaScript to include another JavaScript file

Step 1: Add following function in web page.
<script type='text/javascript'>
//<![CDATA[
function loadScript(url){var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.async = true;
script.defer = false;
head.appendChild(script);}
//]]>
</script>

Step 2: Just call the below loadScript function, Where you want to include the js file.
<script type='text/javascript'>
//<![CDATA[
loadScript('https://code.jquery.com/jquery-2.1.3.min.js');
//]]>
</script>

Method 2: Use jQuery to include another JavaScript file.

Step 1: Add jQuery File in your webpage.
<script src="https://code.jquery.com/jquery-2.1.3.min.js"/>
Don't add this if you are already using any jQuery.js file
Step 2: Just call the  getScript  function functions.
<script>
jQuery(document).ready(function(){
    $.getScript('https://code.jquery.com/jquery-2.1.3.min.js');
});
</script>

Tags:- how to use multiple javascript files, multiple <script> tags in <head>, javascript multiple src, adding more than one javascript to a page, javascript multiple files best practices, html include all javascript files in folder, javascript import all files in directory, combine multiple javascript files into one, Speed Up Your Javascript Load Time, How do you import multiple javascript files in HTML index file without the bloat, How do I include a JavaScript file in another JavaScript file, javascript include html file, javascript call function from another js file, javascript include external js, include all js in one file, include js file in another js file jquery, javascript include js file in html, js import from another file
Share it:

Blogging

Discussions

How To

JavaScript

jquery

Tricks

Troubleshooting

Tutorial

Website

Post A Comment:

0 comments: