页面中js按顺序加载完全的方法

页面中js加载完全的方法

 

function loadScript( url, callback) {

    var script = document.createElement("script");

    script.type = "text/javascript";

    if (script.readyState) {

        script.onreadystatechange = function() {

            if (script.readyState == "loaded" || script.readyState == "complete") {

                script.onreadystatechange = null;

                callback();

            }

        }

    } else {

        script.onload = function() {

            callback();

        }

    }

    script.src = url;

    document.getElementsByName("head")[0].appendChild(script);

}

如何让脚本的执行顺序按照你设定的顺序执行,使用嵌套的方式:

loadScript("file1.js", function() {

    loadScript("file2.js", function() {

        loadScript("file3.js", function() {

            alert("All files are loaded");

        });

    });

});

posted @ 2019-07-27 11:08  不想飞的小鸟  阅读(3128)  评论(0编辑  收藏  举报