How to force drupal to load jQuery in every page (not just ones that use it)?
keff - April 5, 2008 - 22:42
Hello, I am trying to add some jQuery powered goodness to my website, by adding jQuery code directly into header section of page.tpl in my template (in Drupal 6.1). Everything works fine, but only on pages where Drupal needs jQuery (like page with BUeditor, /node/??/edit with rollout menus etc.). On the other ones, jQuery isn't loaded (and i know it's smart default behaviour).
I tried putting
<?php
$js = drupal_add_js('misc/jquery.js', 'core', 'header');
print drupal_get_js('header', $js);
?>instead of
<?php
print $scripts;
?>into header, which worked, but it seems more of a hack to me and I would expect that this can have some nasty side effects (like loading jQuery twice maybe?).
Is there better way to tell Drupal I want to load jQuery on every page, regardless of whether drupal uses jQuery there?

Doesn't look too bad to me
Doesn't look too bad to me ... tho' I've not quite got my head round all the arguments to those functions. I'd have thought
<?phpdrupal_add_js('misc/jquery.js', 'core', 'header');
print drupal_get_js(); // Gets header JS by default
?>
would be sufficient? The 2nd line is how $scripts is defined anyway in http://api.drupal.org/api/function/template_preprocess_page/6.
I guess you've worked out the basic problem that you can't inject drupal.js into $scripts because $scripts is already defined ..! So your method sensibly just adds the jQuery (if not there already) to any other JS needed on the page and then grabs it all.
Unfortunately even implementing mytheme_preprocess_page(&variables) in template.php in your theme (as per http://drupal.org/node/223430) won't help since the template_preprocess_page() has already been run (and hence $scripts has been populated). Your best bet might be just to put the drupal_add_js() in a module hook that is run on every page view. From reading the Description it appears that hook_init() is the way to do this in D6 http://api.drupal.org/api/function/hook_init/6.
gpk
----
www.alexoria.co.uk
Thanks for the link to
Thanks for the link to hook_init()!
I will probably leave it as it is now, where it can be turned on and off in a template file, but the hook_init will surely come handy.
I had no scripts on the page
I had the problem of no javascript scripts being included on the page.
I added scripts[] = scripts.js to my_theme.info ( scripts.js is just a blank file) and it seemed to kick start the js process.
now it includes /misc/jquery.js, /misc/drupal.js and /themes/my_theme/scripts.js
as well as Query.extend(Drupal.settings, { "basePath": "/" });
all of which were missing until i add that blank script to my .info file
...
The documentation says you can put a 'script.js' file in your theme directory and it (and jQuery as well) automatically gets included.
Or, add a "scripts[] = my_script.js" to your theme's .info file.
It might be that you have to make drupal aware of these changes. Maybe visiting the theme administration page would do it. Or clearing Drupal's cache. You may wish to update the handbook pages with details if you gain some useful insights.
Worked for me
Copied misc/jquery.js to my theme directory and renamed to script.js
Worked immediately and perfectly on the next page load.
Yes jquery.js does get loaded twice on some pages as a result, but how can this cause a problem? It all seems to work fine as far as I can see.