javascript - 在 document.ready() 中异步加载外部 JS 文件

标签 javascript jquery asynchronous

我试图在应用程序的 document.read() 事件中异步引用外部 JS 文件。

我目前正在做什么来实现这一目标?

    var s = document.createElement('script');
    s.type = 'text/javascript';
    s.async = true;
    s.src = 'https://xxxx/jquery-3.2.1.js';
    var x = document.getElementsByTagName('script')[0];
    x.parentNode.insertBefore(s, x);

我的问题:

是否有更好的方法使用其他方法以更少的代码行实现相同的目标?

最佳答案

如果您使用 jQuery,那么您可以使用 .getScript 函数。

jQuery.getScript("https://xxxx/jquery-3.2.1.js");

关于javascript - 在 document.ready() 中异步加载外部 JS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44580222/

相关文章:

javascript - 如何通过 http 请求创建具有匹配父 ID 的树结构

jquery - 元素的高度操作

javascript - jQuery on() 事件不会为 sweetalert2 文本框触发

javascript - 在 .then() 中显式调用 resolve()

ios - 等待异步执行的 block 的iOS(或RubyMotion)惯用法是什么?

javascript - 在正确的上下文中捕获异步异常

javascript - 从右到左 jQuery 选择器不起作用

javascript - #(hash) 作为服务器的 GET 参数

javascript - Angularjs 动态搜索表单

javascript - jquery: this.not (':animated' ) && that.is (':visible' ) 不遵守规则,语法问题?只有几行代码