页面中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");

        });

    });

});

相关文章:

  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
  • 2021-07-31
  • 2021-08-29
  • 2022-12-23
猜你喜欢
  • 2021-11-29
  • 2021-06-13
  • 2022-12-23
  • 2021-11-18
  • 2021-06-02
  • 2022-12-23
  • 2021-11-20
相关资源
相似解决方案