0

I have a code for defer loading of javascript I am wondering if the code will load the scripts in order (JQuery.min first) according to where they are in the code ?

function downloadJSAtOnload() {


var element01 = document.createElement("script");
element01.src = "js/jquery.min.js";
document.body.appendChild(element01);

var element02 = document.createElement("script");
element02.src = "js/plugins.js";
document.body.appendChild(element02);

var element03 = document.createElement("script");
element03.src = "js/scripts.js";
document.body.appendChild(element03);


var element04 = document.createElement("script");
element04.src = "js/SmoothScroll.min.js";
document.body.appendChild(element04);

var element05 = document.createElement("script");
element05.src = "js/contact-form.js";
document.body.appendChild(element05);

}

if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;

If this doesn't load JQuery.min first and then the other js files how can you make JQuery.min the first script to load and then the rest after that has loaded? Also I am guessing for example the contact-form.js will be one of the first to finish loading as it's small so this could cause the Uncaught ReferenceError: $ is not defined I'm guessing because the JQuery.min hasn't finished downloading so how would you solved this ? I'm guessing an listener / if finished loading load the others else wait... Thanks in advance

ui-unicorn.co.uk
  • 151
  • 1
  • 5
  • 17

2 Answers2

1

This is quick, but I think it should work.

function downloadJSAtOnload() {
    var element01 = document.createElement("script");
    element01.src = "js/jquery.min.js";
    document.body.appendChild(element01);

    element01.onload = function() {
        var element02 = document.createElement("script");
        element02.src = "js/plugins.js";
        document.body.appendChild(element02);

        var element03 = document.createElement("script");
        element03.src = "js/scripts.js";
        document.body.appendChild(element03);


        var element04 = document.createElement("script");
        element04.src = "js/SmoothScroll.min.js";
        document.body.appendChild(element04);

        var element05 = document.createElement("script");
        element05.src = "js/contact-form.js";
        document.body.appendChild(element05);
    }
}
Jarek Kulikowski
  • 1,399
  • 8
  • 9
1

found here Load jQuery with JavaScript using Promises

function loadScript(url) {
    return new Promise(function(resolve  reject) {
        var script = document.createElement("script");
        script.onload = resolve;
        script.onerror = reject;
        script.src = url;
        document.getElementsByTagName("head")[0].appendChild(script);
    });
}

function loadjQuery() {
    if (window.jQuery) {
        // already loaded and ready to go
        return Promise.resolve();
    } else {
        return loadScript('https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js');
    }
}


// Usage:
loadjQuery().then(function() {
    // code here that uses jQuery
}, function() {
    // error loading jQuery
});
CumminUp07
  • 1,936
  • 1
  • 8
  • 21
  • This looks good, but does the script have to be appended to the head? Why not after the closing body tag? Will this code using promises work cross browser? Another small issue, potentially, Google will punish page load if the full render (on load) takes too long, not just first render. Will this code not infringe on the native onload? This would be an amazing solution, just wondering if it works and how. – ptts Jul 21 '17 at 16:58
  • @ptts I don't think it should matter where it is appended to. according to this https://stackoverflow.com/questions/22516959/how-to-determine-if-a-promise-is-supported-by-the-browser it is cross browser compatible – CumminUp07 Jul 21 '17 at 17:02
  • Wow, this is great then. However, which one of the replies in the link is the one which works? There is some controversy if I read all answers. – ptts Jul 21 '17 at 17:07
  • I looked at the most updated one from december of last year – CumminUp07 Jul 21 '17 at 17:08
  • I assume I use var element05 = document.createElement("script"); etc etc where // code here that uses jQuery is also are there any downsides to this approach vs pure javascript? – ui-unicorn.co.uk Jul 21 '17 at 17:23
  • yes, that is where you would load your other scripts. This is pure javascript – CumminUp07 Jul 21 '17 at 17:28
  • Whats browser support like with promise – ui-unicorn.co.uk Jul 22 '17 at 20:46
  • @Neths according to this https://stackoverflow.com/questions/22516959/how-to-determine-if-a-promise-is-supported-by-the-browser is cross browser compatible – CumminUp07 Jul 22 '17 at 23:18
  • My problem was I loaded scripts after jQuery and some of them have dependencies on others. For such scripts I have put script.async=false; script.defer=true to make them run in order – Svitlana Apr 10 '18 at 11:14