Hey there, I hope this isn't too much of a noob question, but I can't seem to find an answer anywhere. I'm working on a site with other developers and we're transitioning from Drupal 5 to 6. In Drupal 5 this php statement includes /misc/jquery.js

print $scripts;

It looks like things are different now in Drupal 6. jquery.js is still there in /misc but according to Firebug, it's not being loaded in page-front.tpl.php. In fact, if I just echo out $scripts to the body, there's nothing in the variable. Is this normal, or is something broken?

If this is normal, then what is the standard way of including jquery.js for front page use (or any page templates)? I saw that I can include scripts in the theme's info file, but that appears to be theme relative files only... I know I can manually include it in my html's head, but I don't want to start down the wrong path. Better to get it right from the beginning...

Thanks all!

Comments

pragna’s picture

you will get the solution in this link....... http://drupal.org/node/132442
and plz specify which theme aru you used for your project ? I mean to say garland base, Zen or any third party .....?

Pragna J Bhalsod

seanwcom’s picture

It's a 3rd party, custom developed in house for our Drupal 5 installation. We are converting that. It's not based on any existing theme.

Also, I've read through the converting to 6.x documentation. I understand how to use the .info file in our theme, and we are using it already for other scripts... but what I can't find in that documentation is how to specifically load jQuery. Another comment on this thread suggests that jquery is conditionally loaded based on drupal_add_js() - so should I use drupal_add_js() in my theme, calling my jquery plugins so that jquery gets loaded? Or should I just include our jQuery plugins in the .info file and see if jquery gets loaded?

It's that bit of documentation that I can't seem to find anywhere... how do I load my jQuery plugins correctly?

nevets’s picture

You should only put the script in you .info file if you want the script loaded on every page (this will also cause jquery.js to be loaded on every page).

You should use drupal_add_js() from a module (not theme) to conditionally load a javascript file.

nevets’s picture

The file jquery.js is now conditionally loaded. If some module loads a javascript file using drupal_add_js(), the jquery.js is also added.

seanwcom’s picture

Ok, so lets take my front page for example... I have jQuery plugins I want to use there, but not on all pages throughout the rest of the site. So, I should be able to add drupal_add_js() to the head of my page-front.tpl.php, right? Something along the lines of:

drupal_add_js("path/to/some/plugin.js", "theme", "header");

And then doing that, Drupal will load jQuery... I see in the code for the drupal_add_js() function that it will.

So I guess I just want to verify, is this the preferred method rather than putting anything in the .info file?

Thanks for the help!

nevets’s picture

druapl_add_js() does not work from a template file since $scripts is already set. See http://drupal.org/node/304258, more specifically http://drupal.org/node/304255

seanwcom’s picture

Thanks for your help so far Nevets, very much appreciated!

So, back to my original post, $scripts doesn't contain anything. Also, for this particular need, I don't have a specific module created that I can put my jquery plugins into. To be specific, I'm trying to load the plugins necessary for a "scroller" on the landing page (page-front.tpl.php). In Drupal 5, the $scripts variable loaded jquery.js (along with collapse.js, etc) but with Drupal 6, $scripts is empty for me. Anyway, like I said, I don't have a module to put drupal_add_js() into, which is why I was leaning towards putting it in my template.

So, looking at those links you provided, the last paragraph of that page says this:

If you are adding your .js file from within a theme, then you simply need to make the call from within template.php, and don't forget you need to add the second parameter this time as 'theme' so that Drupal knows this script should be loaded after all core and module scripts are loaded:

drupal_add_js(drupal_get_path('theme', 'mytheme') .'/my_js_file.js', 'theme');

Is that not still valid with 6.x? If so, I think that will solve my problems... off to test!

seanwcom’s picture

So what I decided to do in the end is add this to my template.php

 if (drupal_is_front_page()) {
    drupal_add_js('sites/all/themes/themeName/js/plugin.js', 'theme');
    /*
    ... and the others here ...
    */
} 

That seems to be working perfectly and as best as I can tell, is the right way to do things without having a module call drupal_add_js()

Thanks for the help everyone!

AlexandreSantos’s picture

It's help-me too.