I need to upgrade my drupal install first of all and jquery will probably be current. But then I need to a) include jquery on every page, not just some pages like add content, and b) I want to know how to override in the future in case I want to use a newer jquery js than what drupal uses. Help?

Comments

drupalninja99’s picture

I looked around a little further and ended up doing this.

I added a function to the template.php to keep my actual tpl files clean:

function get_scripts(){
	$js = drupal_add_js('sites/all/themes/common/js/jquery-1.2.2.min.js', 'core', 'header');
	unset($js['core']['misc/jquery.js']);
	print drupal_get_js('header', $js);
}

And then in the page.tpl.php I replaced <?=$scripts?> with <?=get_scripts()?> and that seems to work pretty well. Is there a better solution than this?

nevets’s picture

There is the jquery update module which also includes the compatibility file and includes fiixes for some core files. The jquery update module though does not update to jQuery 1.2. One thing to note about using jquery 1,2 with Drupal 5 is that some core js functionality breaks.

drupalninja99’s picture

I stumbled upon a problem the other day that's led to all sorts of frustration. I use couple lines of jquery to make the sidebar height the same as the content bc the sidebar has a pretty background and I want it to be the full height. Simple, ya?

Well I stumbled on the fact that there is a) a bug with the height that makes set the height via height(val) doesn't work with drupal 5's js. Well if I use a newer version like from the jquery site or from drupal 6 the height works but then I have problem b) which is the new js causes the cute little collapsible fieldsets not to collapse. I can expand them but when I click on them again to collapse they don't work. Arghh!

So far now I reverted back to the old js but this is frustrating bc all I want is my height function to work and my collapsible fieldsets to work. I will try your jquery module.

drupalninja99’s picture

The update module did fix the problem, so that's good though the problem is that now I have to enable that module for every site I run off this install, and that seems to be a pain. And I don't know how long it will be supported so ideally I'd like a solution that would work globally.

drupalninja99’s picture

function get_scripts(){
	$js = drupal_add_js('sites/all/themes/common/js/jquery-1.2.2.min.js', 'core', 'header');
	$js = drupal_add_js('sites/all/themes/common/js/collapse-fix.js', 'theme', 'header');#add every time
	$js = drupal_add_js('sites/all/themes/common/js/compat-1.0.js', 'theme', 'header');#add every time

	unset($js['core']['misc/jquery.js']);

	print drupal_get_js('header', $js);
}

I ended up going with this that doesn't a) require me to install a jquery update module and b) not have to overwrite the misc jquery.js file. Instead I will install this function in the template.php for any site where I use jquery functions on every page. Works for me.

elgreg’s picture

I found this really helpful, thanks.

blockedmind’s picture

This does not fix collapse of fieldsets.

drupalninja99’s picture

It does for me? I use it all the time

dman’s picture

I think Jaykali forgot to mention that although he's no longer "using" jquery update, he did steal the above listed fix-up scripts from it. You'll need to get them from the jquery_update distro anyway.
Those lines are pretty much all jquery update does anyway.
This just shows a way of hard-coding its job or letting it be done in a theme, rather than enabling an extra module.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

drupalninja99’s picture

Because that's easily duplicated as I do a bunch of themes. I didn't like the thought of having to enable the module every time. I could have used "profiles" I guess. Thank you for your courage.

Proct0t’s picture

To keep <?=$scripts?> I suggest using a preprocess function in your template.php

Right now i'm using this:

function yourtheme_preprocess_page(&$vars) {
  drupal_add_js("js/jquery-latest.js", 'core'); //where you store your jquery
  drupal_add_js("js/more_javascripts.js", 'theme'); //any other js files you may have

  $js = drupal_add_js(NULL, NULL, 'header'); //get header js files in an array
  unset($js['core']['misc/jquery.js']); //unset default drupal jquery js
  $js['core'] = array_reverse($js['core'], 1); //make our own jquery file first (see note)

  $vars['scripts'] = drupal_get_js('header', $js); //create script tags and set them to $scripts
}

Note that the array_reverse line is to make jquery.js first in the core scripts. Without this jquery.js is last in core files, which breaks drupal.js since it uses jquery. Also note that this could possibly break things if more core js files are added in later drupal releases (it would reverse the order of all core js files).

Proct0t’s picture

I knew array_reverse would be a bad idea... it breaks pages such as the node-edit ones that include tabledrag.js in its core, so replace the line with the one below.

$js['core'] = array_slice($js['core'], -1) + array_slice($js['core'], 0, -1);

feo’s picture

Thanks a lot, clever trick.

drupalninja99’s picture

all i know is that it's a real beating any method you use. with the constant drupal jquery updates id suggest going with the module.

also for many plugins there are modules for porting popular plugins like lightbox that might work better than trying to integrate one yourself.

thezombieguy’s picture

I needed to change my jquery library for one specific page so as not to destroy the rest of my site, and this worked beautifully. I'm going to move it to a module and customize it a bit so that I can incrementally add the new jquery to other pages as they get updated.

Thanks

drupalninja99’s picture

ie collapse is messed up

djg_tram’s picture

In Drupal 7, use hook_js_alter().

jaesperanza’s picture