Benefit of loading JavaScript in template.php rather than the theme info file?

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

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.


Your intuition is generally good.

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

Tags: Theming

Similar questions

css file defined in .info file is not loading in Drupal 7
This is the my .info file details but these css are not loading URL: this is a custom drupal theme under development.
How can I attach an existing server image to an image field rather than uploading a new image file?
Is there a module or some other way of attaching an image to an image field by selecting the image from existing files on the server rather than upload a new image? I'm working on a project where many nodes have an image field. That image is displayed in various places at different sizes using image styles. The users are migrating from another syst...
How can I automatically upload images on file selection rather than pressing the upload button?
I have a custom content type to allow users to upload images. I'm trying to keep the interface as simple as possible. The content type contains a single image field. This works, but some users don't understand that the image has been uploaded because they don't see it after selecting (to preview the upload, the upload button must be pressed). Is th...
What is the benefit of Panels Everywhere
Several websites advocate the benefits of Panels Everywhere (I have put them at the end of this question). However, I have tried the module out out and cannot see the benefits. A regular Panel page gives you the option to Disable Blocks. It also lets you display things like Menu Links, Footers and User Log-ins. So I'm not sure what Panels Everywher...
what benefit is it to clearing the Drupal cache now that I have Varnish in front of Drupal?
Our site has Varnish in front of Drupal for anonymous user page requests so that these pages are cached. I know how to clear the Varnish cache within Varnish but my question is what benefit is it to clearing the Drupal cache now that I have Varnish in front of Drupal?
Benefit of running custom query over views
I have a content type and some nodes. I am displaying latest 10 nodes from these content type through views block display. Is there any benefit if i add a custom SQL query to display latest 10 items. I will use the same SQL query that i got from view.

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.