drupal_add_js(drupal_get_path('theme', 'mytheme') .'/my_js_file.js', 'theme');

I want to add a javascript file just to the tpl file titled


I have this code to enter it to a specific them but what about just a certain tpl file.

I am guessing something like this

drupal_add_js(drupal_get_path('theme', 'mytheme') .'/my_js_file.js', 'view');
  • You can add it in your tpl file too using drupal_add_js. Just flush your caches and it should show up.
    – zarsiwala
    Commented Sep 20, 2011 at 17:06

1 Answer 1


This is what I'm using. I don't know if this will have any gotchas in future where the template file I want is in the array but not actually used. So far it's working flawlessly.

function yourthemename_preprocess_page(&$vars) {
    $templates = $vars['template_files'];
    if (in_array('page-node-add-discipline', $templates)) {
        drupal_add_js(path_to_theme() . '/js/create_discipline.js');
        $vars['scripts'] = drupal_get_js();

Put that in your template.php

  • I edited it with the whole function. That's copy pasted from my code just with the theme name changed.
    – Joren
    Commented Sep 20, 2011 at 23:42

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.