4

I've got a link 'test/nojs' on a page. I've added the class 'use-ajax' to that link, so that Drupal automatically AJAXifies the link and replaces the 'nojs' part with 'ajax'. So far so good.

However, I'd like to hook into the AJAX request as there might be cases where I'd like to stop the request from being sent off to the server. However, I can't get it up and running properly.

One post say that I can do this by setting a 'hook' function in Drupal.ajax[element].beforeSend(a,b) and some places say that I can do it by settings a 'hook' function in Drupal.ajax[element].options.beforeSend(a,b). However, I can't get any of this to work.

Is there a "right" way of hooking into the AJAX process? Even better, is there a "right" way of hooking into the AJAX process for certain elements?

1

You can use the following code,

Drupal.ajax[element].default_beforeSend = Drupal.ajax[element].beforeSend;
Drupal.ajax[element].beforeSend = function(xmlhttprequest, options) {
  if (1) {
    xmlhttprequest.abort();
  }
  else {
    Drupal.ajax[element].default_beforeSend(xmlhttprequest, options);
  }
}
  • Thanks. However, even though I use the above i get the error "Uncaught TypeError: Cannot read property 'beforeSend' of undefined" – sbrattla Nov 29 '12 at 14:04
  • After calling it with Drupal.ajax[element].options.beforeSend, I did get it to fire, but it errors out on xmlhttprequest.abort(); and sends me to site.url/system/ajax, displaying raw ajax. So I can interrupt, but not cancel the submission. – cjspurgeon Jun 27 '14 at 0:16

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.