3

I'm incorporating an external theme into Meteor. It has some scripts loaded before the end of the body tag. The <script> tags doesn't work when inside the <body> of the Meteor template.

The theme uses js like Bootstrap, jquery, and various other probably external JS libraries (which it also puts at the end, before the </body>. The last script is this custom.js script which is this theme-specific. Inside the script other than regular functions statements, there are two anonymous functions, two if blocks at the top level, a $(document).ready callback and a $(window).load callback.

What's the best way to load the scripts with the least tempering? I tried to move all the scripts inside the <head> of the Meteor template. But some effects is missing. I think maybe the $(window).load is not getting called at the right time. So I call it inside Template.scriptAppend.rendered where scriptAppend is a template to a partial right before the end of the </body>. I also move the two anonymous functions and the two if blocks inside the $(window).load. It works but it's such a hassle. Is there a way to leave the script as it is?

Before all these I initially just think I would use jQuery to add the <script> tags in inside Template.scriptAppend.rendered but there seems to be no effect and I have no idea why.

So my questions are, (1) is there a way to leave the script as it is and (2) when is Meteor <head> get processed anyway? Apparently $(window).load is not getting executed at the right time expected by the script.

1 Answer 1

1

Sometimes Meteor loads files in a different order than a normal web server, meaning that your functions won't load as you're used to. Including the external script in the head is just fine, but you should try placing everything that would normally go inside the $(document).ready() function inside a Template.templatename.rendered function instead. That has done the trick for me in the past.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.