views:

579

answers:

4
+2  Q: 

Drupal adding JS

I know this is not a Drupal forum but this forum is quick at responding so I thought I should give it a shot.
Here's my problem. I'm trying to insert reference to the javascript file in the header by using drupal_add_js function. I placed this line inside template preprocess function of template.php. The result....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?
Drupal Version - 6x

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

.....
A: 
  drupal_add_js(path_to_theme().'/js/jquery.cycle.all.js');
  $vars['scripts'] = drupal_get_js();
Nikit
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 - <script type="text/javascript" src="/drupal/misc/jquery.js?d"></script> <script type="text/javascript" src="/drupal/misc/drupal.js?d"></script> <script type="text/javascript" src="/drupal/jquery.js?d"></script> <script type="text/javascript" src="/drupal/drupal.js?d"></script>
Andrew
Did you add this code in phptemplate_preprocess_page?
Nikit
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
@Nikki Yes it's inside phptemplate_preprocess_page
Andrew
@ScottI 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
+1  A: 

Andrew,

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.

Peter Carrero
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
<script type="text/javascript" src="/drupal/misc/jquery.js?4"></script><script type="text/javascript" src="/drupal/misc/drupal.js?4"></script><script type="text/javascript" src="/drupal/jquery.js?4"></script><script type="text/javascript" src="/drupal/drupal.js?4"></script>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
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 <?php print $scripts ?> on page.tpl.php
Peter Carrero
@peterI'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
+3  A: 

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
+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
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
A: 

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.

Mongey