主脚本文件:a.js
var script = document.createElement("script");
script.onload = function() {
alert("加载 脚本b 成功!"); //这里可以看到弹出成功提示。
}
script.src = "b.js";
document.getElementsByTagName("head")[0].appendChild(script);
子脚本文件:b.js
var script = document.createElement("script");
script.onload = function() {
alert("加载 脚本c 成功!"); //这里永远都看不到弹出成功加载的提示!
}
script.src = "c.js";
document.getElementsByTagName("head")[0].appendChild(script);
在 a.js 中动态加载 b.js 脚本,是可以触发加载成功的事件的(说明加载成功);
在 b.js 中再动态加载另一个脚本,就不会触发加载成功的事件了(说明没有加载成功)。
这是为什么呢?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
并没有,会正常运行
路径对吗(凑字数)
b.js 里的 变量名
script换成script_b试试最后找到了原因,这困扰阻碍我两天的问题,竟然是因我在实际的第2个脚本中,将 onload 写成了 onLoad 之误。一个大写字母之差!遗憾 JetBrains 系列的 WebStorm、PhpStorm、IntelliJ 编辑器竟都不能给予代码书写错误提示。想念微软的 Visual Studio了!!
感谢楼上几位兄台的热心解答。你们说的是对的,这样没什么问题,是会正常运行的。