Friday, May 25, 2018

Load multiple javascript/js files from one javascript file

Sometimes there are several javascript files used to include in a project. but js file could be a small file. It's good practice if we use less file in a web project. So, in that case, we can load multiple javascript/js files from one javascript file.

Example
<footer>
<script type="text/javascript" src="jsfile1.js"></script>
<script type="text/javascript" src="jsfile2.js"></script>
<script type="text/javascript" src="jsfile3.js"></script>
</footer>
We need to make a function example function name include also this function will use that single js file where we can call other js files.
Code for the single js file (single.js)
function include(jspath) {
    document.write('<script type="text/javascript" src="'+jspath+'"><\/script>');
}

include("jsfile1.js");include("jsfile2.js");include("jsfile3.js");
Now we can call just one single js file where other 3 js file already include
<footer>
<script type="text/javascript" src="single.js"></script>
</footer>

One important thing to remember that if we need to use the function or this code to combine js file
document.write('<script type="text/javascript" src="jsfilepath.js"><\/script>');

some js file may not work if we use this jquery method.
$.getScript("includes/tableExport/jspdf/jspdf.js", function(){});



No comments:

Post a Comment

Total Pageviews