5

I'm trying to insert reference to the Javascript file in the header by using drupal_add_js(). I placed this line inside the template preprocess function in template.php. The result that the code is not working at all: There is no script link in output as it should be. Can anyone tell me what am I doing wrong?

function phptemplate_preprocess_page(&$vars) {
    $url = drupal_get_path("theme","mysite");  
    drupal_add_js($url."/jquery.js");  
    drupal_add_js($url."/drupal.js");  

.....
apaderno
  • 28,547
  • 16
  • 75
  • 90
Andrew
  • 1,035
  • 7
  • 22
  • 40

5 Answers5

10

Even easier, Javascript that needs to be loaded on all pages can be added in the theme's .info file. See http://drupal.org/node/171205#scripts.

marcvangend
  • 5,592
  • 4
  • 22
  • 32
  • 1
    +1 interesting... the theme info file is a requirement of drupal 7, the link you posted also says that drupal 6 loads all .js files from the theme folder by default. the drupal_add_js can be called from modules and even from nodes if you have the php input format enabled... – Peter Carrero Apr 02 '10 at 15:59
  • 1
    No, that's not completely correct. The .info file for a theme is required in both Drupal 6 and Drupal 7. D6 only auto-loads the file called 'script.js'. D7 does not auto-load js files at all. Like you say, drupal_add_js() can be called from a node, but that is not recommended because you're mixing content with presentation. – marcvangend Apr 02 '10 at 17:44
2
  drupal_add_js(path_to_theme().'/js/jquery.cycle.all.js');
  $vars['scripts'] = drupal_get_js();
Nikit
  • 5,128
  • 19
  • 31
  • So, does it mean drupal_add_js function doesn't add script tag? Because that was my understanding of drupal help. That said, I did as you suggested and, what happened was that, it added those files(drupal.js, jquery.js) twice. :confuse: Here's how it looked - – Andrew Apr 02 '10 at 01:34
  • 2
    drupa.js and jquery.js are already added by Drupal core. So adding them to your theme is creating duplicates. It's possible to remove the defaults, but not a good idea. If you're using different versions, it's likely to break something in Drupal that relies on the versions that come with core (which is fine if you know what you're breaking and are okay with that, but you don't seem to know). And if you're using the same versions, what's the reason for moving them? – Scott Reynen Apr 02 '10 at 13:01
  • @Nikki Yes it's inside phptemplate_preprocess_page – Andrew Apr 02 '10 at 13:09
  • @Scott I was under that impression as well for the book I'm reading never mention about to add those two files manually. When I view the output through mozilla "page source", there is no script reference there. So I have no choice but to add manually. – Andrew Apr 02 '10 at 13:12
1

If you place the javascript file in the theme directory, you can just add the following to the themes .info file

scripts[] = myJavaScriptFile.js

After you add this file you need to deactivate your theme and then reactive it.

Conor Mongey
  • 1,097
  • 1
  • 7
  • 16
1

As pointed by other, simply using drupal_add_js() from a hook_preprocess_page() implementation doesn't work. The references to JavaScript files collected through the multiple calls to drupal_add_js() are used to generate the corresponding markup into the $scripts variables from template_preprocess_page(). But a theme's implementation of hook_preprocess_page() is always called after template_preprocess_page(). So in order to have the files added through drupal_add_js() in your .tpl.php file(s), you need to override the already set $scripts variables:

function THEME_preprocess_page(&$variables)
  drupal_add_js(...);
  $variables['scripts'] = drupal_get_js();
}

But, you shouldn't have to add jquery.js and drupal.js yourself, it should already be done automatically by Drupal core. If you need to do it yourself, then something is broken on your site. You can (re-)add the files as a quick fix, but you better find the root cause of the issue as it is most likely creating other issues you haven't yet identified (or worked around without realizing it).

Pierre Buyle
  • 4,883
  • 2
  • 32
  • 31
  • +1, and you can also add JS files in your own module's `hook_init()` or `hook_preprocess()` or `hook_preprocess_page()` implementation (or similar hooks). – Sk8erPeter Apr 05 '12 at 09:43
-1

drupal_add_js() works, but you are putting it deep into the page rendering process. I suggest you put it in the template.php like you are doing, but in the beginning, outside any function. This is what we did on a few of our projects.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Peter Carrero
  • 1,596
  • 2
  • 13
  • 13
  • You are right. Once I take it out from the function and placed it outside in the global scope, it works like a charm.:) I have another question though. it just purely knowledge sake. Why drupal add those files twice from two different locations in the output?any idea? Here's how it looks - – Andrew Apr 02 '10 at 13:25
  • As suggested by the user - scott, I stopped using those files under my theme folder and used drupal defaults instead. So there is no path mention in drupal_add_js function;in other words, just file name - drupal_add_js('jquery.js') – Andrew Apr 02 '10 at 13:25
  • andew, those 2 files should automatically get added to the theme by drupal. the whole collection of js files are added to the page by the on page.tpl.php – Peter Carrero Apr 02 '10 at 15:57
  • I've been told the same thing several times. But I can't seems to find it in the output unless I add manually. I don't know what is wrong. :confuse: – Andrew Apr 02 '10 at 17:10
  • 3
    Adding the call outside any function outside of any function is bad practice (it clutters the code and make it harder to maintain) and an ugly workaround. There are better ways to deal with this issue. – Pierre Buyle Oct 31 '11 at 11:43
  • I totally agree with @PierreBuyle (+1 for the comment), using `drupal_add_js()` outside any functions is a really bad way to add one or more JS files in Drupal. It should be added e.g. in a module's `hook_init()` implementation, in your theme's `YOURTHEME_preprocess_page()` or `YOURTHEME_preprocess_node()` function, or using similar functions, where the addition of JS files is somehow "predictable". – Sk8erPeter Apr 05 '12 at 09:40