Skip to content

Instantly share code, notes, and snippets.

@gavinblair
Created April 7, 2011 15:14
Show Gist options
  • Save gavinblair/907973 to your computer and use it in GitHub Desktop.
Save gavinblair/907973 to your computer and use it in GitHub Desktop.
Use the latest jQuery with your Drupal theme
<?php
function {theme_name}_preprocess(&$vars, $hook) {
if (arg(0) != 'admin' && $hook == "page") {
// Get an array of all JavaScripts that have been added
$javascript = drupal_add_js(NULL, NULL, 'header');
// Remove the original jQuery library and Drupal's JS include
unset($javascript['core']['misc/jquery.js']);
unset($javascript['core']['misc/drupal.js']);
// Add in our new jQuery library and Drupal's JS include
// We do it this way to keep the includes in the same order
$core = array(
//Alternative jQuery
drupal_get_path('theme', '{theme_name}').'/js/libs/jquery-1.5.1.min.js' => array(
'cache' => TRUE,
'defer' => FALSE,
),
'misc/drupal.js' => array(
'cache' => TRUE,
'defer' => FALSE,
)
);
// Merge back into the array of core JavaScripts
$javascript['core'] = array_merge($javascript['core'], $core);
// Rerender the block of JavaScripts
$vars['scripts'] = drupal_get_js(NULL, $javascript);
}
}
?>
@gavinblair
Copy link
Author

This can break things. Be sure to only do this on the frontend of your site. It can mess up admin toolbars if they are dependent on Drupal's version of jQuery.

@SeanJA
Copy link

SeanJA commented Apr 7, 2011

Man... I hate that drupal is still stuck in the past with jquery 1.2.6...

Does this work well? It doesn't seem like it would necessarily keep the order proper, unless the only two js files that core includes are jquery.js and drupal.js

@gavinblair
Copy link
Author

Not sure, I just added it to my new theme. I'll let you know how it goes. Already it is breaking the Admin Toolbar, but it is time to look for an alternative toolbar anyway

@SeanJA
Copy link

SeanJA commented Apr 7, 2011

Instead of removing jQuery and Drupal.js, why not instead just add another jQuery:

$core = array(
//Alternative jQuery
drupal_get_path('theme', 'linamar').'/js/libs/jquery-1.5.1.noconflict.min.js' => array(
'cache' => TRUE,
'defer' => FALSE,
)...);

Where jquery-1.5.1.noconflict.min.js = [minified jQuery source] + var $j = jQuery.noConflict();

Not the greatest solution, but it will not bust things on the front end that rely on jQuery 1.2.6

@SeanJA
Copy link

SeanJA commented Apr 7, 2011

Also, that should be jquery 1.5.2 should it not?

@gavinblair
Copy link
Author

Adding a second jQuery in another namespace was suggested, but I don't like the thought of loading jQuery twice - it's small but not that small.

I'm not sure, 1.5.1 is what came with the HTML5 Boilerplate.

@gavinblair
Copy link
Author

My goal is to have the frontend completely independent of Drupal styles and scripts.

@SeanJA
Copy link

SeanJA commented Apr 7, 2011

My goal is to have the frontend completely independent of Drupal styles and scripts.

A lofty goal, but there are lots of modules that use javascript on the front end...

@SeanJA
Copy link

SeanJA commented Apr 7, 2011

Which admin menu are you using?

@gavinblair
Copy link
Author

I was using a module called Administration, but now I'm using a module called Toolbars

@gavinblair
Copy link
Author

wow Toolbar is really bad - switching to SimpleMenu. Working fine so far!

@SeanJA
Copy link

SeanJA commented Apr 11, 2011

totally using this on http://i.seanja.com so I can use a lightbox plugin to show the images full size

@SeanJA
Copy link

SeanJA commented Apr 12, 2011

Hmmm... it doesn't really work with compression turned on...

@gavinblair
Copy link
Author

Does it include it? Does it include Drupal's jQuery instead?

@SeanJA
Copy link

SeanJA commented Apr 12, 2011

I think it might be, I am going to try it out again when I get home to see what it is actually doing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment