Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
markus_petrux CreditAttribution: markus_petrux commentedI'm not sure to understand what needs to be changed. Could you please attach a patch?
Comment #2
Cristhian CreditAttribution: Cristhian commentedSorry 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:
And I think it should be like this:
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.
Comment #3
Cristhian CreditAttribution: Cristhian commentedBy the way.. oops.. it's javascript not php, I was wrong at the top..
Comment #4
markus_petrux CreditAttribution: markus_petrux commentedOh, 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.
Comment #6
sbusch CreditAttribution: sbusch commentedI 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:
Comment #7
markus_petrux CreditAttribution: markus_petrux commented@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