gpt4 book ai didi

javascript - 无法动态加载 JS(没有 jQuery)

转载 作者:行者123 更新时间:2023-11-30 13:27:21 27 4
gpt4 key购买 nike

我使用此代码,但未加载 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));

注意事项

  • 在 document.onload 之后
  • 在谷歌浏览器中
  • 控制台没有错误

注释2

在@john_doe 评论中发现,脚本代码被注入(inject)页面(注入(inject) html->head),但未加载(如图所示在网络选项卡中)。什么会导致这种情况?

最佳答案

它说它加载了一个对象:

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 = function () {
alert('typeof window.gapi.plusone === ' + typeof window.gapi.plusone); // this
};
script.src = prefix + 'apis.google.com/js/plusone.js';
head.appendChild(script);

关于javascript - 无法动态加载 JS(没有 jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8020088/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com