I am using a zen subtheme and I am trying to add some javascript using the drupal_add_js on a certain node. When I use the drupal_add_js from within the html--node--XX.tpl.php file, it does not work.

But when I use it in the page--node--XX.tpl.php it does. I don't understand what causes the difference in the behavior. I thought that this would be the same. These are the lines I am adding in both cases:

drupal_add_css("sites/all/libraries/shadowbox/shadowbox.css", $type = 'file', $media = 'all', $preprocess = FALSE);
drupal_add_js("sites/all/libraries/shadowbox/shadowbox.js", array('type' => 'file', 'scope' => 'header', 'weight' => 5));
drupal_add_js("Shadowbox.init({skipSetup: true});", array('type' => 'inline', 'scope' => 'header', 'weight' => 6));

Can anyone illuminate me here?

4 Answers 4


Using drupal_add_js in a variant of the html template will never work, because the styles are printed in the html template. At the time you call drupal_add_js the variable containing the styles have already been created and you are too late to alter it with drupal_add_js.

The reason it works in page templates is that it is parsed before the html template so the variable containing the styles that is printed in the html template has not yet been generated.

  • Ah! This makes sense. This means that I can use it to page--node--xx.tpl.php or node--xx.tpl.php etc but never on a html.tpl.php variant, right?
    – nikan
    Commented Jan 24, 2012 at 13:24
  • @nikan True. You could do it in a html.tpl.php but it would require that you rebuild the $scripts variable and did it before it was printed. In other words a lot more complicated.
    – googletorp
    Commented Jan 24, 2012 at 13:47

You can try to insert the JS files by the preprocess function in your theme's template.php file:

Here goes an example:

function MYTHEME_preprocess_page(&$variables) {
  drupal_add_js(drupal_get_path('theme', 'MYTHEME') .'/mytheme.js', 'file');

You can use an if() to validate the current path in the $variables in order to prevent the script to be loaded on every page.

To debug the $variables values you can install the devel module an use the dpm() function.

For example:

function MYTHEME_preprocess_page(&$variables) {
  • I have tried successfully the preprocess solution for the whole site. It was in the 'if' part that I got confused. Did not know which variable to check. Still unsure. Normally, in a template I would find the nid from a url argument. Does this work in the template.php file? Thanks anyway.
    – nikan
    Commented Jan 24, 2012 at 16:12
  • 1
    You can use the current_path() function to validate the url to load the script. Like: if(current_path() == 'node/X') drupal_add_js(drupal_get_path('theme', 'MYTHEME');
    – revagomes
    Commented Jan 24, 2012 at 16:44
  • 1
    I suppose you mean drupal_add_js(drupal_get_path('theme', 'MYTHEME') .'/mytheme.js', 'file'); Yes, this is good! Thanks.
    – nikan
    Commented Jan 24, 2012 at 17:13
  • Exact. Sorry about that. When I realized that I could no longer edit the comment to fix this error.
    – revagomes
    Commented Jan 24, 2012 at 17:36

Structure is

So it is like html--page--node(--XX).tpl.php

Simply use node--XX.tpl.php but make sure there is a html.tpl.php, page.tpl.php and node.tpl.php file in your subthemes directory.

  • I am confused: you are suggesting that I use three files? I tried using the html--page--node(--XX).tpl.php but it returns a blank page.
    – nikan
    Commented Jan 24, 2012 at 13:13
  • Simply use node--XX.tpl.php and ignore the structure
    – JurgenR
    Commented Jan 24, 2012 at 14:02

If you are looking to add javascript to individual nodes the best option is Code Per Node

  • 1
    Useful. Thanks. But in this case I would rather do it through the core drupal functions in the template, than through a module.
    – nikan
    Commented Jan 24, 2012 at 16:14

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.