I have two JS files that for some reason causes weird bugs in drupal even with menus it should not effect, like the admin menu. My solution is to have the JS files load only on the page I need it to.
However, I have found mixed messages on the forum and am unsure as to whether I can use Drupal_add_js in a template file or if it can go in a page.tpl file built for my page.

I got my JS from here because I had to build a custom menu that i couldn't do with views and the accordion module.... Anyway, here it is: http://www.unwrongest.com/projects/accordion/

I would really just like one example of how to use drupal_add_js properly. Where to put it on the page and such.
please be detailed, I really suck as js.



mattyoung’s picture

Hi Becky,

drupal_add_js() definitely do not work in page.tpl.php because by the time this template is rendered, all the header information is already expanded so calling drupal_add_js()/drupal_add_css() will have no effect. For the same reason, these calls do not work in hook_preprocess_page() either. However, these calls should work in other templates.

beckyjohnson’s picture

So, if I want to load these specific js pages on one page alone, I have to load it in the template.php?

How would I go about writing this into the template.php?


mattyoung’s picture

You should not do it with template.php because it's better not to modify theme. My preference is to leave theme alone.

One way to do this is with a module and hook_init().

A second way is with a block and use the PHP Filter. Enable the PHP Filter module (admin/build/modules), create an Input format with the PHP filter (admin/settings/filters). Then create a block (admin/build/block), use the Input format with the PHP filter, in the body of the block(include the <?php ?> tags):

if (arg(0) == 'abc' && arg(1) == 'def' ....) {
  drupal_add_js(blah blah);

show this block in any region, doesn't matter what region because it's not going to be visible as it has no output.

An alternative is take advantage of block visibility rule:

in the block body:

drupal_add_js(blah blah);

in the block visibility config, set the block visibility to the path(s) where you want the js included.

beckyjohnson’s picture

Thanks. So basically if i add it in a block the way you say, the js will only load on the page the block shows up on?
Just checking.
Also in your above example what is the 'abc' and 'def' representing?

Update: I took your advice about the block but it actually printed out the js on the page:

drupal_add_js(drupal_get_path(‘theme’, ‘wimax_forum’) . ‘/jquery-1.2.1.min.js’, ‘theme’);
drupal_add_js(drupal_get_path(‘theme’, ‘wimax_forum’) . ‘/menu.js’, ‘theme’);

Is this correct syntax? I thought it was.


mattyoung’s picture

You have to use the PHP filter and include the <?php ?> tags enclosing the whole block body.

>Also in your above example what is the 'abc' and 'def' representing?

See arg()

beckyjohnson’s picture

It didn't work.

This is my code in the block:

<?php>drupal_add_js(drupal_get_path(‘theme’, ‘wimax_forum’) . ‘/jquery-1.2.1.min.js’, ‘theme’);
drupal_add_js(drupal_get_path(‘theme’, ‘wimax_forum’) . ‘/menu.js’, ‘theme’);?>

php was the imput filter and my node that I want it to work on is saved in full html which worked when I had the js attached via the info file.

Update: I found my syntax error.

beckyjohnson’s picture

Ok. So even with my fixed syntax error it still didn't work.
I searched around and found another format which did work.


what do you think of this? Why do you think the other drupal_add_js_format didn't work?



mattyoung’s picture

>what do you think of this? Why do you think the other drupal_add_js_format didn't work?

<?php>.....?> is wrong.

is it:

<?php ... ?> like your second sample

beckyjohnson’s picture

Yeah, what i mean is even with my second example with the drupal add js in this formatdrupal_add_js(drupal_get_path(‘theme’, ‘my_theme’) . ‘/myjs.js’, ‘theme’); did not work.

I wonder if the path is wrong?


mattyoung’s picture

what does drupal_get_path(‘theme’, ‘my_theme’) return?

do echo drupal_get_path(‘theme’, ‘my_theme’) in the block body and see what what comes out of the block display. Everything looks correct, you just need to debug it.