如何在meteor中包含来自CDN的JavaScript? [英] How to include JavaScript from a CDN in meteor?

查看:45
本文介绍了如何在meteor中包含来自CDN的JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在包含我自己的客户端脚本之前将 CDN 中的 JS 包含在 Meteor 中,以便客户端脚本可以依赖它.

I'd like to include JS from a CDN in Meteor before including my own client scripts so that the client scripts can depend on it.

...
<script type="text/javascript" src="https://ajax.googleapis.com/..."></script>
...
<script type="text/javascript" src="/client/..."></script>
...

我尝试通过 *.html 文件和 标签包含脚本.但似乎来自 *.html 文件的标题内容将始终附加到 HTML 标题的末尾,无论我将它放在文件层次结构中的哪个位置(例如,将文件放在 lib 文件夹中或在客户端 JS 文件之前按字母顺序排序)不会有帮助).

I tried including the script via *.html file and between <head> tags. But it seems that header content from *.html files will always be appended to the end of the HTML header, no matter where I place it in the file hierarchy (e.g. placing the file in a lib folder or sorting it alphabetically before client JS files won't help).

有什么想法可以让我客户端脚本之前包含来自 CDN 的 JS 而不必构建智能包?

Any ideas how I could include JS from a CDN before client scripts without having to build a smart package?

推荐答案

假设您不需要在 Meteor 包之前加载这些文件,请创建一个在任何其他包之前加载的 JS 文件.Meteor 按字母顺序加载文件,因此它必须是第一个加载的文件.为此,将其命名为 aaLoadCDN.js 就足够了.通过向文档 head 中添加 script src 元素来动态加载 CDN 脚本:

Assuming you don't need to load these files before the Meteor packages, create a JS file which is loaded before any of the others. Meteor loads files in alphabetical order so it must be the first file loaded. To that end, naming it aaLoadCDN.js should suffice. Dynamically load the CDN scripts by adding a script src element to the document head:

var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');  // optional
script.setAttribute('src', 'url/to/the/cdn/script.js');
document.getElementsByTagName('head')[0].appendChild(script);

以下是一些从 CDN 加载脚本的真实 Meteor 包:

Here are some real-world Meteor packages loading scripts from CDNs:

  • snapsvg
  • Font-Awesome (CSS).

这篇关于如何在meteor中包含来自CDN的JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆