How to execute javascript from within an ajax request OK, I'm doing some basic validation inside of an ajax form callback. I'd like to use the result to execute some javascript. What I've tried looks something like this:

inside my form alter:

if (isset($form_state['values']['field_temp_age'])) {
  if (validate())
  {
    //...return product information
  } else {
    drupal_add_js('alert("Validation fails!");', 'inline');
  }
}


This doesn't work. In this case, nothing is returned.

Also, tried this from inside the ajax callback:

function my_ajax_callback(&amp;$form, &amp;$form_state) {
  if (!validate()) drupal_add_js('alert("Foo!");', 'inline');
}


Not sure what's the approach here. How can I execute arbitrary javascript based on information returned from an ajax request? 

Thanks.
Not sure of how you are making the AJAX call, but jQuery AJAX calls such as $.ajax let you execute a callback after the result has been returned. I recommend returning FALSE or a code indicating an error to your calling function and placing your "validation failed" logic inside of that in the success handler.