0

I want to load a javascript file but do not want a "<script type="text/javascript" src="..."></script>" tag created for it. I am using iron-router. So, I want this js file to execute in 'onAfterAction' hook, i.e. after the template is loaded.

I am thinking of following ways to do this:

  1. One way is place this file in "public" directory and use '$.getScript' function to load and execute this on demand. But, these files won't get minified and compacted to one file. Also, this will need another fetch on the n/w.
  2. The other way is to create a package and include this file in the package. But, then I will have to enclose the whole file in a function and 'export' this function in package. And execute this function on demand.
  3. The third way is to put these files in 'client' directory and call "$.getScript('/client/js/...". But, the js file would get executed twice as a "<script type="text/javascript" src="..."></script>" tag will be created for it..

Is there a better way of doing this so that the file is locally available and can be executed on demand?

Anish Singh
  • 881
  • 1
  • 13
  • 33
  • Wrapping the code inside a function and only call that function when you need to? Why don't you want to load it at start? If it contains sensitive information, you might want to put it in the /server folder. – user3557327 Nov 17 '14 at 17:14

1 Answers1

0

From what I understand about Meteor. All JavaScript executes without having to load a <script></script> tags. Depending on how you seem okay with it being a public script, I'd put it in the client folder and utilize the Template.[your-template-name].rendered in some way where you can load that particular script after the template is rendered.

http://docs.meteor.com/#/basic/Template-onRendered

Lieuwe Rooijakkers
  • 164
  • 1
  • 4
  • 14
Lex
  • 36
  • 3