Resolving jQuery plugin dependencies in Drupal without touching the version in core (or using jQuery Update)
NOTE: The jQuery Multi module provides a way to implement this method using a UI or hooks.
Recent versions of jQuery include a
noConflict() function which restore the previous definition of the
$() function and, optionally, the
This provides a way of using later versions of jQuery for particular purposes, without affecting the version that core and contrib are using.
Problem: We want to use the "foo" jQuery plugin in our theme, but it needs jQuery 1.3.
Solution: In our theme's .info file, carefully specify the order of the scripts:
- Load the jQuery 1.3 library
- Load the "foo" plugin library
- Load and execute a custom "noConflict" script
scripts = [...]
scripts = scripts/jquery-132.js
scripts = scripts/jquery.foo.js
scripts = scripts/jquery-noconflict.js
scripts = [...]
jquery-noconflict.js looks like this:
var jQuery13 = jQuery;
How it works
Drupal loads its own jQuery library
Load the new jQuery library
When jQuery 1.3 loads, it copies any pre-existing definitions of
$()to 'backup' variables
_$, before defining its own replacement versions.
$()now refer to jQuery 1.3
Load any plug-ins which require jQuery 1.3
jQuery plug-ins attach themselves as methods to the global
jQueryobject. Because the global jQuery is (for the moment) jQuery 1.3, this means the plug-in will attach itself as a method available to jQuery 1.3 (and not available to the original jQuery).
var jQuery13 = jQuery;copies the current definition of
jQueryto a non-conflicting name.
jQuery.noConflict(true);tells jQuery to restore the global definitions of
$()to whatever they were before jQuery 1.3 loaded.
This means that they once again refer to the original core jQuery library supplied with Drupal, while our new jQuery13 object remains in existence, along with the plugins that attached themselves to it.
Use the plugin via the jQuery13 object
Because the global names have already been tidied up, it doesn't matter when this code runs.
jQuery13()instead of using