I have js file which needs to be attached to template.php file.
Can I directly add the js file in template.php file ?
I am using drupal_add_js() but
should I add it inside the functions of template.php
should I add it directly into template.php file
I have the code in d6 which has added the drupal_add_js() in template.php file
without including it in any of the functions?

2 Answers 2


Hope this will help you.

In Drupal 7, you can achieve it by

  • Using preproccess function(theme_preprocess_page)

    function MYTHEME_preprocess_page(&$variables) {
        //check your condition and add js here
        drupal_add_js(drupal_get_path('theme', 'MYTHEME') .'/mytheme.js', 'file'); //if js file is in theme folder
        drupal_add_js(drupal_get_path('module', 'MODULE_NAME') .'/js/myscript.js', 'file'); //if js file is in module folder

Quick example of use in your theme's template.php

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

Reference : https://api.drupal.org/comment/14344#comment-14344

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.