We have a Rails application where we are including our application dependencies in the html head within application.js
:
//= require jquery
//= requi
This code will load each script URL put in libs
in the exact order, waiting for one to fully load before adding the next one.
It's not as optimised than letting the browser doing it, but it allow you to monitor the errors and force the order of the loading.
(function(){
var libs = [
"http://example.com/jquery.js",
"http://example.com/tracker.js",
"http://example.com/myscript.js"
];
var handle_error = function() {
console.log("unable to load:", this.src);
};
var load_next_lib = function() {
if(libs.length) {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = libs.shift();
script.addEventListener("load", load_next_lib);
script.addEventListener("error", handle_error);
document.body.appendChild(script);
}
};
load_next_lib();
})();
But I would advise you to check every tag of your website and see if they have a
defer=""
or async=""
attribute.
Most issues come from these because they tell the browser to execute the script later.
They may also just be in the wrong order.