In Drupal.AjaxLoad.loadFiles, behaviors are attached twice when new external scripts are found..

          if (!found) {
            $.getScript(src, function () {
              newExternalScripts++;
              Drupal.AjaxLoad.externalScripts[Drupal.AjaxLoad.externalScripts.length] = src;
              Drupal.AjaxLoad.loadComplete(index, target, response);
            });
            Drupal.settings.ajaxLoad.loadPending[index]++;
          }
    if (newExternalScripts == 0) {
      Drupal.attachBehaviors(target);
      // Ensure inline scripts are parsed after all external scripts have loaded.
      Drupal.AjaxLoad.loadInline(response);
    }

The first part should leave the line "newExternalScripts++;" out of the getScript caller, like this:

          if (!found) {
            newExternalScripts++;
            $.getScript(src, function () {
              Drupal.AjaxLoad.externalScripts[Drupal.AjaxLoad.externalScripts.length] = src;
              Drupal.AjaxLoad.loadComplete(index, target, response);
            });
            Drupal.settings.ajaxLoad.loadPending[index]++;
          }

Comments

markus_petrux’s picture

Status: Active » Needs work

I'm not sure to understand what needs to be changed. Could you please attach a patch?

Cristhian’s picture

Sorry about that.. I'm on windows and I haven't been able to make patches..

But is very simple, in ajax_load.js, line 46 there is this:

            $.getScript(src, function () {
              newExternalScripts++;

And I think it should be like this:

            newExternalScripts++;
            $.getScript(src, function () {

Because the function() inside $.getScript is in another thread, and when you check "if (newExternalScripts == 0)" it is always zero, the line "newExternalScripts++;" hasn't been executed.

Cristhian’s picture

By the way.. oops.. it's javascript not php, I was wrong at the top..

markus_petrux’s picture

Status: Needs work » Fixed

Oh, I see. Thanks!

Committed to CVS. I'm using the loadPending array, and I have moved it to the Drupal.AjaxLoad object to prevent it from being altered by Drupal.settings.

I have also committed a few more things to CVS, minor, so it would better to wait until the next development snapshot is built, or check out from CVS HEAD.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

sbusch’s picture

Status: Closed (fixed) » Needs review

I think this is still open, even for 6.x-1.x-dev (is the fix propagated from CVS HEAD to the latest devel snapshot?)

We have the same issue (Drupal behaviors attached twice. Second call to behavior triggered by loadFiles() JavaScript function, with a jQuery selector (string) as context). We upgraded to 6.x-1.x-dev but the issue was still there. Deactivating the ajax_load module solves the problem.

Content of "ajax_load.info" from our setup:

; $Id: ajax_load.info,v 1.2 2009/09/20 14:01:06 markuspetrux Exp $
name = Ajax Load
description = Helper module to load and add JavasScript and CSS data for Ajax-loaded content.
package = User interface
core = 6.x

; Information added by drupal.org packaging script on 2010-07-11
version = "6.x-1.x-dev"
core = "6.x"
project = "ajax_load"
datestamp = "1278833379"
markus_petrux’s picture

Status: Needs review » Closed (fixed)

@sbusch: it shouldn't be a problem to invoke Drupal.attachBehaviors more than once during the life time of the page, IF the behaviors are coded correctly (processed DOM elements are marked as processed, etc.). In fact, that's the way Drupal 6 is supposed to work.

What we fixed here is that it was not really necessary to invoke Drupal.attachBehaviors more than once, which is not a source of troubles with other modules per-se.

If you have a problem with the implementation of any Drupal behavior when it is invoked more than once, then please review the code in that behavior so that it works properly in such situations. Please, see http://drupal.org/node/205296