I have (drupal 7) view where I have pagination with Ajax enabled. When any pager item is clicked I want to perform certain thing after the view is reloaded with ajax. I am trying the following code:

Drupal.behaviors.myBehavior = {
    attach: function (context, settings) {
    $(document).ajaxComplete(function( event, request, settings ) {
        console.log("Ajax Completed");


There is no output form console.log(). What am I doing wrong ?

  • You're missing 1 line, after the last line, you should have another line with })(jQuery); – No Sssweat Jun 1 '18 at 0:05
  • Thanks for pointing out. Updated now. It was missing while pasting the code. It's there in the actual site but still not working. – Sandesh Yadav Jun 1 '18 at 3:51
  • I think in this case, since you're using .ajaxComplete you have to do it without the Drupal behaviors, so remove line 2 and 3. – No Sssweat Jun 1 '18 at 17:06

I found a solution here. Based on @djayp34's answer, this is how I achieved what I wanted.

Basically the idea is: Drupal does not support adding a JS snippet through drupal_add_js() or any other function at AJAX callback. So what we are doing here is:

Step 1: We register/load that particular JS code during normal page load. In template.php:

function MYTHEME_preprocess_html(&$vars) {
    'Drupal.ajax.prototype.commands.myFunction = function(ajax, response, status){
       //The JS code that needs to be called after completion of ajax

Step 2: We call that JS code when we want it. Here we want it to be called during AJAX callback.

In module file.

function MYMODULE_views_ajax_data_alter(&$commands, $view){
  if($view->name == 'MY_VIEW'){
    $commands[] = array("command" => 'myFunction'); 
| improve this answer | |

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.