My drupal version is 4.7 and I am using k2 theme.
In k2 theme's page.tpl.php, under header if I add

<?php print drupal_add_js('path-to/javascript.js') ?>


It doesn't output the corresponding code to include the javascript. what gives ?
If I include the javascript outside the PHP , ie. with <script type="text/javascript" src="...
then it works.

Anybody got a clue, why drupal_add_js is not working.

Also how do I get to include certain javascripts only on selected pages.

Comments

avpaderno’s picture

drupal_add_js() doesn't output the corresponding code to include the javascript, but it adds a line on the section of the HTML page, so the script is included and ran when the page is loaded.

I don't think the function is thought to be called from a theme, but if it is then you simply put a line like

<?php drupal_add_js('path-to/javascript.js') ?>

-- Kiam

gumnaam1’s picture

that's what I meant. Sorry for not being clear about my message earlier.

when I do <?php drupal_add_js('path-to/javascript.js ?>
It shold add a line in the header section of html , like <script type="text/javascript" src="/path-to/javascript.js" />
but this is not happening.
Hence the javascript is not included and in turn not downloaded by the browser.

--
http://www.bhaskarvk.info

avpaderno’s picture

If you reported the line as you really wrote it, then you forgot to close the string and the parenthesis.
That is the only thing I could think of, as the rest seems right.

Anyway, did you check that all the drupal files got uploaded correctly in your server, and that they didn't got truncated?

-- Kiam

avpaderno’s picture

... you can look at http://drupaldocs.org.

-- Kiam

thumb’s picture

Adding JavaScript functions to a theme with drupal_add_js() seemed so simple when I read about it, but, as I found out, placement is everything.

I'm building a new theme and couldn't understand why adding drupal_add_js() to my page.tpl.php wouldn't work. I created a template.php file for my theme and added drupal_add_js() to it.

Problem solved (Finally).

macm’s picture

Hi thumb

May you explain what do you do in template.php? May you post code of template.php and mypage.tpl.php (just to know how you call a javascript)

giorgosk’s picture

In drupal5 the call to drupal_js_add has changed to drupal_add_js
and you call it in your template.php

drupal_add_js("/themes/themename/jsfile.js");

Look in the http://api.drupal.org/api/5/function/drupal_add_js
for more details

------
GiorgosK (kongeo previous username)

Web development/design blog part of the world experts network

------
GiorgosK
Web Development

d.deman’s picture

The call

drupal_add_js("/modules/glossary/pop-up.js");

didn't work for drupal 4.7 but if you change it to

code>
drupal_add_js("modules/glossary/pop-up.js");

it does work.

ThePeach’s picture

Thanks a lot, that fixed it
someone should take care of drupal api reference docs and underline this error :)

FrontBurner’s picture

Also keep in mind that based on your performance settings any attached js file could be aggregated into a file. So if you are trying to confirm the existence of the reference by viewing source and looking for a reference to your js file you may not see it. Doesn't mean it isn't there though.