0

I am trying to override Drupal.ajax.prototype.beforeSend like this:

(function($) {
  // In mymodule.js, which appears after ajax.js in the page build
  Drupal.ajax.prototype.beforeSend = function(xmlhttprequest, options) {
    console.log('overridden!')
  }
})(jQuery);

The file that contains the code is listed below core's ajax.js in the page's source, yet it's the console.log I put inside core's ajax.js beforeSend that is showing up.

What else is to it? Has anything been changed in Drupal? Using version 9.3. Can't override any of those functions.

4
  • Did you also pass in the 'Drupal' object?
    – Kevin
    May 2, 2022 at 14:15
  • I even passed 'jQuery, Drupal, drupalSettings'.
    – KrzysiekK
    May 2, 2022 at 14:17
  • 2
    Isn't ajax spelled Ajax?
    – 4uk4
    May 2, 2022 at 14:19
  • Wow, actually there are both "ajaxes", and the one I need is, indeed, spelled 'Ajax'. Thank you! Can't mark the comment as the answer though.
    – KrzysiekK
    May 2, 2022 at 14:22

1 Answer 1

1

There are two javascript objects:

  1. Drupal.ajax
  2. Drupal.Ajax

You have to override the prototypes in Drupal.Ajax. For each Ajax object declared in Drupal settings an instance of it will then be stored in Drupal.ajax.instances.

1
  • Do you know where this is documented? I looked around a little and I mostly just find answers to questions like this, with information like this, but no linked source.
    – sonfd
    May 2, 2022 at 15:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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