There are some services (like FB like or AddThis) that provide a snippet of code. It looks like
<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.
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<head>
Now here are the problems and questions:
<head>
-- because of the speedAnd surprisingly you cannot use template helpers / variables in the <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 server templates.
So, how should I do this in Meteor? Let me repeat:
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...
To include an external JavaScript file, we can use the script tag with the attribute src . You've already used the src attribute when using images. The value for the src attribute should be the path to your JavaScript file. This script tag should be included between the <head> tags in your HTML document.
External scripts cannot contain <script> tags.
External Javascript should not contain tag.
Internal JavaScript: JavaScript can be added directly to the HTML file by writing the code inside the <script> tag . We can place the <script> tag either inside <head> or the <body> tag according to the need. External JavaScript: The other way is to write JavaScript code in another file having a .
<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.
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.
My solution is use packages. See https://github.com/meteor/meteor/tree/master/packages/spiderable for more details.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With