//方法1----------------------------------------
document.write("<script src='js/external.js'><\/script>");
//方法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);
}
两种方法动态加载外部原生JS文件
原创
©著作权归作者所有:来自51CTO博客作者挚爱的强哥的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
vue项目导入外部css样式和js文件
vue项目导入外部css样式和js文件
vue 导入 css样式 js文件 -
js 读取外部的本地json文件
-- https://www.cnblogs.com/streamwang/p/8056301.html
json 读取文件 html 数据 i++ -
js 导入json配置文件
匹配好路径
JS 导入 JSON json