Long version: Drupal.behaviors is not just a replacement for jQuery.ready, since the latter only works once (when the DOM is ready for manipulation): the behavior can be triggered several times during page execution and can be triggered whenever new DOM elements are inserted into the document.
In addition, modules can override or extend existing behavior (for example, if one of the modules has the behavior of adding a bounce effect on all links, the second module can replace the behavior with another bounce effect).
Short version: it is more modular, although the documentation can be improved.
In addition, starting with Drupal 7, parameters defined using drupal_add_js (PHP) or in Drupal.settings.modulename (Javascript) are passed directly as the second parameter (the first one of which is the context) to the behavior.
For example:
Drupal.behaviors.changeLinks = function(context, settings){ if (!settings) settings = Drupal.settings.changeLinks; $("a", context).hover(function() { $(this).css('color', settings.color); }); };
And if one of your script (or others) creates new nodes, it can still apply behavior to new nodes, not knowing which other modules were installed:
var newNodes = $('<a href="#">Hello</a> <a href="#">World</a>').appendTo('#someDiv'); Drupal.attachBehaviors(newNodes);
wildpeaks Oct. 15 '10 at 10:54 2010-10-15 10:54
source share