drupal_add_js within AJAX callback How can i add some data to drupal setting object using drupal_add_js withing some ajax callback?

For example I have ajaxified flag link on my page, and have implemented hook_flag in my module, how can I add JS within this hook?

function hook_flag($action, $flag, $content_id, $account) {
...
  drupal_add_js(array(
      'my_module' =&gt; array('color' =&gt; 'green')
    ),
    'setting'
  );
...
}
drupal_add_js doesn't work properly in an AJAX callback. If you need to execute a JavaScript function within an AJAX callback, here's a way :

First, in your form, you have to create (you can just import it) the JavaScript function :

function my_module_form($form, &amp;$form_state) {
// [...]
  drupal_add_js(
    'Drupal.ajax.prototype.commands.myFunction = function(ajax, response, status) {
      // Whatever you want to do.
    }',
    'inline'
  );
// [...]
  return $form;
}


Then, in your AJAX callback, you need to call this function you created :

function my_module_my_ajax_callback($form, &amp;$form_state) {
// [...]
  $commands[] = array("command" =&gt; 'myFunction',);
// [...]
  return array('#type' =&gt; 'ajax', '#commands' =&gt; $commands);
}


Hope it helps !