且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何加载没有脚本标记的Javascript文件?

更新时间:2023-12-05 09:52:46

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

js.type = "text/javascript";
js.src = "path_to_the_file.js";

document.body.appendChild(js);

但这是异步的,所以你必须等到加载脚本才能使用它 -

but this is asynchronous, so you have to wait until the script is loaded to use it -

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

js.type = "text/javascript";
js.src = "path_to_the_file.js";

js.onload = function() {
 //Code using this script here
};

document.body.appendChild(js);