I have index.html file where I have Jquery library and bootstrap library for js being loaded in <head>
tag, they are reported as the reason for render blocking, where, jquery.min.js takes 932 ms and bootstrap.min.js takes 765 ms to load. Here, I decided to load the jquery and boostrap files after loading the page following this article. But since I had other dependent JS files as well, I decided to create a Javascript array and iterate it to create the dynamic script tags. Which is doing its part, but the page doesnt load because other dependent files doesnt find the jquery file.
I believe this is happening because, before jquery.min.js gets loaded completely other files gets loaded and hence it breaks.
Note: since all the js files are from the codebase, It doesnt make sense to use defer or async.
Below is the code I am using in the bottom of the page before </body>
tag.
<script type="text/javascript">
var source = [
"common/scripts/external/jquery-3.2.1.min.js",
"common/scripts/external/bootstrap.min.js",
"common/scripts/other/utility.min.js?v=1.0.0",
"common/scripts/external/jquery.ui.widget.min.js",
"common/scripts/external/jquery.fileupload.min.js",
"common/scripts/external/jquery.fileupload-process.min.js",
"common/scripts/external/jquery.fileupload-validate.min.js",
"common/scripts/external/jquery.knob.min.js",
"common/scripts/external/jquery.payform.min.js"
]
function downloadJSAtOnload(source) {
for (var i = 0; i < source.length; i++) {
var element = document.createElement("script");
element.src = source[i];
document.body.appendChild(element);
}
}
if (window.addEventListener) {
window.addEventListener("load", downloadJSAtOnload(source), false);
} else if (window.attachEvent) {
window.attachEvent("onload", downloadJSAtOnload(source));
} else {
window.onload = downloadJSAtOnload(source);
}
</script>
The errors when I load the page:
Please suggest whats the right way to solve the render blocking and if this is the right direction, how I can allow Jquery to load first before every other files get loaded