How to add confirmation to ajax link? I am rendering ajax link as follows:
l(t('Click here'), 'mypath', array('attributes' =&gt; array('id' =&gt; 'my-id', 'class' =&gt; array('use-ajax'))));

How to display confirmation dialog before ajax request is called?
I do it as follows in javascript, like I have found on this thread:
  Drupal.behaviors.module = {
attach: function() {  

 Drupal.ajax['my-id'].beforeSerialize = function () {  

      if(confirm('Are you sure?'))
          return true;
      else
          return false;

  }
 }
}

When I click on link, ajax request is not called but the dialog appears. It is ok. The problem is, that after I click &quot;Cancel&quot; in confirmation dialog, Ajax request is then called after closing the confirmation dialog.
Can anybody help, how to get the confirmation dialog work?
I don't think this is the best way to go about it as you are messing directly with Drupal's Ajax object.

The best way to do this, is to basically write your own click handler and handle the confirmation yourself, then trigger a Drupal AJAX call yourself.

Here is an example: 

l(t('Click here'), 'mypath', array('attributes' =&gt; array('id' =&gt; 'my-id', 'class' =&gt; array('toms-ajax'))));


In your js file:

(function($) {

Drupal.behaviors.tomsAjaxLinks = {
  attach: function(context, settings) {
    $('.toms-ajax',context).once('toms-ajax').on('click', this.handleAjax);
  },
  handleAjax: function(e) {
    // Cache the anchor link
    var $element = $(this);

    // We need some unique id, either ID of link or create our own
    var nowStamp = new Date().getTime() + Math.floor(Math.random() * (1000 - 0) + 0);
    var base = $element.attr('id') || 'toms-ajax-'+ nowStamp;

    // Change the event type to load, so we can trigger it ourselves
    var drupal_ajax_settings = {
      url : $element.attr('href'),
      event : 'load',
      progress : {
        type: 'throbber',
        message : '',
      }
    };

    // Create the ajax object
    Drupal.ajax[base] = new Drupal.ajax(base, this, drupal_ajax_settings);

    // Your confirmation code e.g. Jquery UI Dialog or something
    // Open dialog
    if(yes) {
      $element.trigger('load');
    } else {
      // Dont trigger ajax
    }
  }
}; 

})(jQuery);


Essentially what this does is:


Attaches the handleAjax function to your ".toms-ajax" links once to ensure we do not attach multiple event handlers
On click, the handleAjax function is called
A corresponding Drupal Ajax object is created with it's own unique ID.
It is attached to the link and triggered on the event 'load'. You then
handle the confirmation box.
If the confirmation box confirms the ajaxcall, we simply trigger the 'load' event on the link object and therefore make the Drupal AJAX object do it's thing.