How to reload custom javascript file after Drupal makes ajax call?

How can I reload a custom javascript file after an Ajax call ends?

In my case, I have a custom JavaScript file for managing view data and filters. It works great the first time the page loads.

But as soon as the user sets the filter and clicks Apply ie after calling Ajax, JavaScript seems to stop working. He is there, but does not register any event.

I read some articles on Drupal.org, but this did not give me a solid solution.

Note. This is not a module .js file, but a separate standalone .js file in the theme folder.

+3
source share
3

JavaScript Drupal, any Drupal.bahaviors.

, JavaScript Drupal.behaviors, AJAX, , .

+3

: " js "

:

:

$form['letterdrop_id'] = array(                       
    '#type' => 'select',                              
    '#title' => 'Letterdrops',                        
    '#options' => $letterdrops,                       
    '#prefix' => '<div id="replace_div_ld">',         
    '#suffix' => '</div>',                            
    '#ajax' => array(                                 
      'callback' => 'agc_ems_form_map_change',        
      ),                                              
    );                                                

:

function agc_ems_form_map_change($form, &$fstate) {              
   return array(                                                 
    '#type' => 'ajax',                                           
    '#commands' => array(
      // this command is to reload a form element                                        
      ajax_command_replace("#agc_map", render($form['map'])),    
      // this command does the business and calls my custom function, 
      // with a parameter object supplied
      array('command' => 'afterAjaxCallbackExample',             
          'selectedValue' => 'i am not a fish',                  
    )                                                            
  ));                                                            
}                                                                

js

(function($, Drupal) {                                                      
  // put function into drupal commands:   
  Drupal.ajax.prototype.commands.afterAjaxCallbackExample = 
     function(ajax, response, status) {
    // response object as passed in our Ajax callback          
    // do what you want in here
    alert(response.selectedValue);                                          
  };                                                                        
}(jQuery, Drupal));      

100% jaypan

+1

script, document.createElement( "script" ), innerHTML. . http://www.phpied.com/javascript-include-ready-onload/ , .

-1
source

Source: https://habr.com/ru/post/1757023/