Javascript Meteor JS:使用外部脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14389766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Meteor JS: use external script
提问by Guard
There are some services (like FB like or AddThis) that provide a snippet of code. It looks like
有一些服务(如 FB like 或 AddThis)提供了一段代码。看起来像
<div class="service-name" data-something="x"></div>
<script type="text/javascript" src="http://service-domain.com/service-name.js"></script>
OK, cool, so normally you paste it to your HTML and it works. Not with Meteor.
好的,很酷,所以通常你把它粘贴到你的 HTML 中,它就可以工作了。不与流星。
Here's what I see:
这是我所看到的:
<script>inside of template / body is not loading-- I don't see it in Resources, something in Meteor is actually preventing browser from recognizing it as a JS file- it works from
<head>
<script>模板/正文内部未加载- 我在资源中没有看到它,Meteor 中的某些内容实际上阻止浏览器将其识别为 JS 文件- 它从
<head>
Now here are the problems and questions:
现在这里是问题和问题:
- I don't want loading it from
<head>-- because of the speed - Even if I load it from there -- we have QA and PROD environments. They mustload this script from different domains (like service-domain-qa.com vs. service-domain.com)
- 我不想从
<head>- 因为速度加载它 - 即使我从那里加载它——我们有 QA 和 PROD 环境。他们必须从不同的域加载这个脚本(比如 service-domain-qa.com 与 service-domain.com)
And surprisingly you cannotuse template helpers / variables in the <head>.
令人惊讶的是,您不能在<head>.
With traditional frameworks it's not a question at all - you can include scripts anywhere and they just load; you can use logic / variables in any part of you servertemplates.
使用传统框架,这根本不是问题——你可以在任何地方包含脚本,它们只是加载;您可以在服务器模板的任何部分使用逻辑/变量。
So, how should I do this in Meteor? Let me repeat:
那么,我应该如何在 Meteor 中做到这一点?让我再说一遍:
- I need some external scripts (hosted on 3rd party domain) to be loaded into my app page
- Saving this script into my project's folder is not an option
- Script path depends on the environment (we already have the settings system), so the place of the template that renders it should be passed some data from the code
- 我需要将一些外部脚本(托管在第 3 方域上)加载到我的应用程序页面中
- 保存该脚本到我的项目的文件夹是不是一种选择
- 脚本路径取决于环境(我们已经有了设置系统),所以渲染它的模板的位置应该从代码中传递一些数据
I know the way to achieve this with dynamic script loading from my code (with LAB.js or whatever) on Template.created, but this is so much an overkill...
我知道通过在 Template.created 上从我的代码(使用 LAB.js 或其他任何东西)加载动态脚本来实现这一点的方法,但这太过分了......
采纳答案by Rahul
<script>tags in body or templates aren't executed by Meteor, they are parsed and then handled by Meteor's templating system. You can't expect a script tag in either of those to just work as it would with a normal HTML page.
<script>正文或模板中的标签不会由 Meteor 执行,它们会被解析,然后由 Meteor 的模板系统处理。您不能指望其中任何一个中的脚本标记像普通 HTML 页面一样工作。
The solution is to use Template events (where you could manually append the script tag to the body or something) or load it dynamically like you said. It's not overkill, it's how Meteor works - remember, there is no traditional HTML page or body, there's just the Meteor API, and the Meteor API specifies that in order to load and execute external scripts, you must use the appropriate API methods.
解决方案是使用模板事件(您可以在其中手动将脚本标记附加到正文或其他内容)或像您说的那样动态加载它。这不是矫枉过正,这就是 Meteor 的工作方式——记住,没有传统的 HTML 页面或正文,只有 Meteor API,而 Meteor API 指定为了加载和执行外部脚本,您必须使用适当的 API 方法。
回答by vixh
My solution is use packages. See https://github.com/meteor/meteor/tree/master/packages/spiderablefor more details.
我的解决方案是使用包。有关更多详细信息,请参阅https://github.com/meteor/meteor/tree/master/packages/spiderable。
Package.describe({
summary: "External script"
});
Package.on_use(function (api) {
api.use(['templating'], 'client');
api.add_files('external_script.html', 'client');
});
<head><script type="text/javascript" src=""//mc.yandex.ru/metrika/watch.js""></script></head>
回答by Kuba Wyrobek
If you are using IronRouter you can load external scipt using this package: https://github.com/DerMambo/wait-on-lib
如果你使用 IronRouter,你可以使用这个包加载外部 scipt:https: //github.com/DerMambo/wait-on-lib
Router.map( function () {
this.route('codeEditor',{
waitOn: IRLibLoader.load('https://some-external.com/javascript.js')
});
});
回答by Rune Jeppesen
Why not use jQuery's getscript?
为什么不使用 jQuery 的 getscript?
http://api.jquery.com/jquery.getscript/
http://api.jquery.com/jquery.getscript/
You can add a callback function
您可以添加回调函数
回答by cazgp
You could use something like yepnopeto load the script asynchronously. I use this to load leaflet as and when I need. I'm starting to move over to loading more scripts via yepnope, so that my application renders the bare minimum on initial page load. I place the yepnope stuff inside Template.created.
您可以使用类似yepnope 的东西 异步加载脚本。我使用它在需要时加载传单。我开始转向通过 yepnope 加载更多脚本,以便我的应用程序在初始页面加载时呈现最低限度。我将 yepnope 的东西放在 Template.created 中。
回答by Brent Noorda
Using iframe and the public directory was a hack I used to get script code embedded. In this it was for google adwords code and I did this my main html template:
使用 iframe 和公共目录是我用来嵌入脚本代码的一种技巧。这是用于 google adwords 代码的,我在我的主要 html 模板中做了这个:
<iframe src="/gads.html?v={{{unique}}}" seamless width="160" height="600"
scrolling="no" frameborder="0" marginheight="0" marginwidth="0"
style="margin:0;padding:0;border:none;width:160px;height:600px"></iframe>
and then in the public directory put an gads.html file with my google adwords code, like this:
然后在公共目录中放置一个带有我的 google adwords 代码的 gads.html 文件,如下所示:
<html>
<head>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div>
<script type="text/javascript"><!--
google_ad_client = "ca-pub-54*********";
google_ad_slot = "66******";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>
</body>
</html>
that worked to get the code on the page, although it's far from ideal (for one thing, I think it violates google's terms of service).
这有助于在页面上获取代码,尽管它远非理想(一方面,我认为它违反了谷歌的服务条款)。
回答by Amir Samakar
I'm using METEOR 1.0. I have put all external SCRIPT tags inside a DIV element right before the tag in the layout template. Meteor recognizes them without any issue and they are loaded by browser.
我正在使用 METEOR 1.0。我已将所有外部 SCRIPT 标签放在布局模板中标签之前的 DIV 元素中。Meteor 可以毫无问题地识别它们,并且它们由浏览器加载。

