Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor load scripts, CSS specific to pages

The problem I'm currently experiencing is, that I want to be able to execute only specific scripts and CSS files, because if executed on a wrong page, it produces errors in the browser console.

I'm using "Iron router" for Meteor with only the basic code to make it work. Now, is there a way for me to send scripts as parameters, so it only loads the ones I want the page to load?

like image 710
n00b Avatar asked Aug 30 '13 19:08

n00b


4 Answers

It's true that there's not an official way of doing this yet, but there are some effective ways of doing this. Take, for example, how I load Disqus into a blog section.

Template.blog.rendered = function() {  
  setTimeout(function(){
     var disqus_shortname = 'disqus_shortname'; // required: replace example with your forum shortname
     var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
     dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
     (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
  })
}

Which is taken from Josh Owen's blog. The difference is I wrap this is a timeout so that it blocks to wait on the template to actually be rendered.

What this script does is create a script element, set its src and append it as a child to either the head or body of the template. Again, it only does so after the template is rendered so that means all of your elements are available to your script.

If you're using Iron Router you can do the same thing with onAfterAction. The key is to append the new script as a child node in your dom. Hope this helps.

like image 33
thatgibbyguy Avatar answered Nov 06 '22 12:11

thatgibbyguy


In short, there isn't. (Yet.)

You probably have a wrong code structure if you've got errors. There are ways to execute code only when it's needed - you should probably take a look at template.rendered and template.created callbacks, as well as iron router controllers. However, that's for execution - everything is loaded on the beginning.

That being said, you technically could use things like require.js to load some scripts dynamically. But this negates most of Meteor's advantages. It's also quite difficult to make it work properly with Meteor.

like image 185
Hubert OG Avatar answered Nov 06 '22 11:11

Hubert OG


WARNING: NOT THE BEST SOLUTION

OK, this is what I did. I put CSS and JavaScript files on the public folder, then in the <head> I wrote this little script:

$(function() {
    if(location.href.indexOf("http://yourpage.com/page") > -1) //where page is the url you want to server the files
        $('head').append('<link rel="stylesheet" href="/mobile.app.css" type="text/css" />');
});

While this is not at all the best way to do this, this could be accomplished in a "hacky" way.

But it worked.

like image 1
ncubica Avatar answered Nov 06 '22 10:11

ncubica


You can use Preloader. It worked for me :).

like image 1
Tomas Romero Avatar answered Nov 06 '22 12:11

Tomas Romero