无法动态加载 JS(没有 jQuery)
我使用这段代码,但 JavaScript 没有加载。
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
var prefix = this.is_current_https ? "https://" : "http://";
script.type = 'text/javascript';
script.onload = fnCustomHandler;
script.src = prefix + 'apis.google.com/js/plusone.js';
head.appendChild(script);
甚至听不到 script.onreadystatechange 也不会生成任何输出。
当我调试 console.log(script) 时,我看到整个元素已正确创建,但未加载引用的脚本。
我也尝试过 head.appendChild(script.cloneNode(true));
Notes
- document.onload 之后
- 在google chrome 中的
- 控制台中没有错误
Notes 2
在 @john_doe
评论中发现,脚本代码被注入页面(进入 html->head),但未加载(如 Networking 中所示)选项卡)。什么会导致这种情况?
I use this code, but javascript is not loading.
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
var prefix = this.is_current_https ? "https://" : "http://";
script.type = 'text/javascript';
script.onload = fnCustomHandler;
script.src = prefix + 'apis.google.com/js/plusone.js';
head.appendChild(script);
not even listening to script.onreadystatechange
is generating any output.
When I debug console.log(script)
I see whole element created properly, but referenced script is not loaded.
I tried too head.appendChild(script.cloneNode(true));
Notes
- after document.onload
- in google chrome
- no errors in console
Notes 2
Discovered on @john_doe
comment, script code gets injected into page (into html->head), but not loaded (as seen in Networking tab). What can cause this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它说它加载一个对象:
It says that it loads an object:
我尝试了它,错误是
fnCustomHandler
未定义,如果我采取它加载的那一行完美。编辑 - 我尝试过
,它工作正常,所以你的函数一定有问题
I tried it and the error is that
fnCustomHandler
is undefinded, if i take that line of it loads perfectly.EDIT - i tried
and it works ok so there must be a problem in your function