jQuery noconflict

Certain aspects of Drupal will break if you update jQuery beyond a certain version. So, to use a higher version for your custom code and still use the old version for Drupal stuff (overlays, tabledrag, etc), you can take advantage of jquery noConflict() http://api.jquery.com/jQuery.noConflict/. It can be a little confusing if you have never used it before. Here's a breakdown.

  1. page loads "jquery.versionX.js"
  2. $ and jQuery belong to versionX
  3. you call your "jquery.versionY.js"
  4. now $ and jQuery belong to versionY, plus _$ and _jQuery that belongs to versionX
  5. var my_jQuery = jQuery.noConflict(true);
  6. now $ and jQuery belong to versionX, _$ and _jQuery are probably null, and my_jQuery is versionY

**drupal.info example**

scripts[] = [...]
scripts[] = scripts/jquery-1.7.1.min.js
scripts[] = scripts/foo.js
foo.js looks like this:

jQueryNoConflict = jQuery.noConflict(true);
 
(function ($, Drupal, window, document, undefined) {
  Drupal.behaviors.jeffythemer = {
    attach: function () {
 
      // Your jQuery code here
 
    }
  };
})(jQueryNoConflict, Drupal, this, this.document);

Tags

Internal References

Article Type

General