The order of script loading

http://stackoverflow.com/questions/18350051/script-not-getting-loaded-in-order

by tokkonoPapa

HTML

<div id="msg"></div>

JavaScript

window.appendText = function (t) {
    var d = document,
        e = d.createElement('p'),
        n = d.createTextNode(t);
    e.appendChild(n);
    d.getElementById('msg').appendChild(e);
}

var script1st = function () {
    appendText('1st');
};
var script2nd = function () {
    appendText('2nd');
};
var script3rd = function () {
    appendText('3rd');
};

script1st.js = encodeURIComponent("(" + script1st.toString() + ")();");
script2nd.js = encodeURIComponent("(" + script2nd.toString() + ")();");
script3rd.js = encodeURIComponent("(" + script3rd.toString() + ")();");

(function (scripts) {
    // Dynamic script injection into DOM will load script asynchronously.
    function createScriptTag(element, index, array) {
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.src = element;

        // Opera 12: doesn't support async, so always synchronously.
        // IE9, Firefox23, Chrome29, Safari5: if false, synchronously.
        // @link: http://www.w3.org/TR/html5/scripting-1.html
        // @link: http://www.w3.org/TR/html-markup/script.html
        /* script.async = false; //*/

        // 'insertBefore()' or else makes no sense if asynchronously.
        document.body.appendChild(script);
    }

    (scripts || []).forEach(createScriptTag);
})([
    "/echo/js/?delay=3&js=" + script1st.js,
    "/echo/js/?delay=2&js=" + script2nd.js,
    "/echo/js/?delay=1&js=" + script3rd.js
]);