0

The title says it all really. Whats the benefit of loading JavaScript files in the template.php file over the theme info file?

For the sake of simplicity lets assume there is no condition to them being loaded. As a themer my instinct is to want to load things with the info file, but this isnt the standard practice on my current project.

1

No real advantages.

There are two reasons to add Javascript : to make some elements work, or to make them look cool.

JS to make something work

These should be #attached to the element they make work. Preferably in the module that created said element. Or they may be written in unobtrusive way and put in info file of this module. Either way, they should never be put in theme, makes no sense to have them there. So these should be out of your question's scope.

Eye candy

These belong to theme all right. And because they do, they shouldn't depend on any script that's in module. If you have a rare situation when they do, then you need to add them in PHP for conditionals that'll cache and not waste browser's resources. But you told no conditions, so this is moot. Or should be, if your theme just assumes presence of modules without checking you have serious bug there, problems waiting to happen.

Having them added in PHP allows some reordering etc. In my experience it was always easier to write my JS in a way that didn't require this. Drupal Behaviors are good for this. If for some reason you can't write your scripts in order independent way, ie you have to use external scripts or have no time for rewrite, then #attached is the way to go.

Other "advantage" would be to attach them only to elements that are supposed to get their cool effects. But this rarely is an advantage. If all you need is to make sure JS is included, include it always. Better for caching and it'll act only on elements with specific id or class anyway, no real harm for browser. Exception here is when you need to take some php variables and make them avaliable to Javascript. Then info file will obviously not suffice and you need to add it via php.

TL;DR

Your intuition is generally good.

| improve this answer | |
0

module.info loads it for all pages unlike template.php where you can specify when and to what page you want to display it.

| improve this answer | |
  • This is serious disadvantage when it comes to caching, and hardly any advantage for good js code at all. Please elaborate, because now it doesn't seem to answer the question. – Mołot Jan 20 '16 at 7:15
0

The only benefits I can think of by using the drupal_add_js() function in template.php is you can set a weight/scope to load the file in a different order if you wanted to, or load it with a scope group or you can load the file from a different location outside of your theme if you wanted to. Other than that I don't see any other benefits.

More about drupal_add_js

| improve this answer | |
  • 1
    drupal_add_js shouldn't be used any more...go with #attached in a render array as replacement – Clive Jan 19 '16 at 13:50
  • Yes, You are right regarding Drupal 8, but I believe this question is referring to Drupal 7, which is fine to use the function. – Rick Bergmann Jan 19 '16 at 13:55
  • 1
    You should always use #attached, even in Drupal 7 - it has better caching support, and means your code will work when you upgrade rather than having to be rewritten. I'll try to dig out the official drupal.org post about this, I know there is one somewhere – Clive Jan 19 '16 at 13:58
  • That's a fair point, future-proofing is good. References will be great. Thanks :) – Rick Bergmann Jan 19 '16 at 14:02
  • 2
    What Clive said is actually quite valid for Drupal 7 too. If you can use #attached that means Drupal has not yet cached the page array, and all added JavaScript will then be cached as well. If you want to test it, implement a form_alter, add some JS, and submit the form with values that wouldn't validate. The second page load will not have the JS unless you used #attached. – AyeshK Jan 19 '16 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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