12

I've got two different modules which in many ways work together. One of the things they work together on is that module A loads content via AJAX, and module B needs to attach event listeners to the content loaded.

However, it appears that when content is loaded via AJAX by module A, only the 'attach' function of Drupal.behaviours.a gets invoked, and not the 'attach' function of Drupal.behaviours.b. Both functions gets invoked on page load, but not on subsequent AJAX content loading.

// This runs every time module A loads new content.
Drupal.behaviors.a = {
  attach: function(context, settings) {
    alert('a:attach');
  }
}

// This only runs on page load, but never when module A loads new content.
Drupal.behaviors.b = {
  attach: function(context, settings) {
    alert('b:attach');
  }
}    

How do i make the 'attach' function of Drupal.behaviours.b run every time any module loads new content?

8
  • Have you tried adding Drupal.attachBehaviors(); to the ajax success callback?
    – Clive
    Commented Sep 9, 2012 at 12:36
  • No, but should that not be taken care of automatically? I thought that was the whole idea of the 'attach' function; that it should be called on page load and page update? It does seem, however, that only the 'attach' function for the invoking module gets called on page update though?
    – sbrattla
    Commented Sep 9, 2012 at 13:27
  • That was my understanding yes, I haven't ever looked into it too deeply though. I know there's been times when after an AJAX call I've needed to reattach behaviours so I just assumed it was required
    – Clive
    Commented Sep 9, 2012 at 13:31
  • Looking at drupal.js : "Drupal.attachBehaviors is added below to the jQuery ready event and so runs on initial page load. Developers implementing AHAH/Ajax in their solutions should also call this function after new page content has been loaded, feeding in an element to be processed, in order to attach all behaviors to the new content." It does seem that I need to invoke the attachBehaviours myself. But; it puzzles me that module A's 'attach' function runs even though I haven't invoked attachBehaviours in the AJAX success callback...? Why just for the invoking module, and not all modules?
    – sbrattla
    Commented Sep 9, 2012 at 13:33
  • 1
    If you want a catch-all solution $('body').ajaxSuccess(Drupal.attachBehaviors); will probably work
    – Clive
    Commented Sep 9, 2012 at 13:43

2 Answers 2

4

This, from the comments in misc/drupal.js, explains how it's supposed to work:

  • Drupal.attachBehaviors is added below to the jQuery ready event and so
  • runs on initial page load. Developers implementing AHAH/Ajax in their
  • solutions should also call this function after new page content has been
  • loaded, feeding in an element to be processed, in order to attach all
  • behaviors to the new content.
2

Try adding this in Drupal.behavior.b:

Drupal.attachBehaviors($(this));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.