异步加载js,兼容IE

  let loadScript = function (url,callback) {
                let script = document.createElement('script');
                script.type = "text/javascript";

                if(script.readyState){  //ie
                    script.onreadystatechange = function () {
                        if(script.readyState == "complete" || 
                                       script.readyState == "loaded"){
                            tools[callback]();
                        }

                    }
                }else {
                    script.onload = function () {
                        tools[callback]();
                    }
                }
                script.src = url;
                document.head.appendChild(script);
            }




            loadScript("demo.js",'test')

 

//demo.js

let tools = {
    test : function () {
        console.log('a');
    }

}

 

posted @ 2021-02-27 16:09  快乐的咸鱼  阅读(151)  评论(0编辑  收藏  举报