首页 > 两种方法动态加载外部原生JS文件

两种方法动态加载外部原生JS文件

    //方法1----------------------------------------document.write("");//方法2----------------------------------------/** 动态加载JS @param {string} url 脚本地址@param {function} callback  回调函数 */function loadJS(url, callback) {let script = document.createElement("script");script.type = "text/javascript";script.src = url;if (typeof (callback) === "function") {script.onload =script.onreadystatechange = function () {if (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") {callback();script.onload = script.onreadystatechange = null;}};}document.querySelector("html").appendChild(script);}

更多相关: