我试图在下面的代码中添加更多脚本,这迫使JS在页面内容之后加载。我想添加script2.js,script3.js等。任何帮助将不胜感激。
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "/home/site/script1.js";
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else
window.onload = downloadJSAtOnload;
答案 0 :(得分:2)
只需修改您的加载脚本功能,例如:
function downloadJSAtOnload(scripts) {
for (script in scripts) {
let element = document.createElement("script");
element.src = scripts[script];
document.body.appendChild(element);
}
}
let scripts = ["/home/site/script1.js","/home/site/script2.js","/home/site/script3.js"];
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload.bind(downloadJSAtOnload, scripts), false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload.bind(downloadJSAtOnload, scripts));
else window.onload = downloadJSAtOnload.bind(downloadJSAtOnload, scripts);