I know the list of Ajax framework commands. Every one of them is doing something useful. But sometimes I want, for debug, include a piece of debug data in my AJAX response. I want it to be unobtrusive, so var_dump is out. I need to wrap my debug data in some kind of command. On the other hand, I will view it using JS console, so I don't want it to actually change anything on my page. And changing something hidden with CSS is not feasible - I'm not in charge of CSS here. So it leaves me with commands approach.

Which command could I use for this purpose?

I was thinking about ajax_command_append with selector that will never be found, but I don't want to introduce any additional errors in my JavaScript Console.

Debugging it with xdebug is not, currently, possible, and I would rather avoid spending time and resources on creating VM, when what I need is something I can do with simple var_dump on non-AJAX pages.


As far as I know, there isn't such logging or "nulling" Ajax command. That doesn't mean you can't build any! I for example, have created some custom Ajax commands, and also a eval() so I can run some quick codes.

1: Define the JS action

You need to add a new JS file, or merge as necessary. Note this document.ready event is important (this is just the definition, so no need to be a behavior).

    $(function() {
        Drupal.ajax.prototype.commands.js_eval = function(ajax, data, status) {
        Drupal.ajax.prototype.commands.console_log = function(ajax, data, status) {

2: The Ajax render array

Assuming you are aware of the ajax_render() and JS renderable arrays, it's time to create your own custom command.

function mymodule_ajax_command_js_eval($script) {
  return array(
    'command' => 'js_eval',
    'script' => $script,
    'my_custom' => 'my custom value',
function mymodule_ajax_command_console_log(&$var) {
  return array(
    'command' => 'console_log',
    'var' => $var,

Above PHP function is not really a hook, and can be any name you want. For the sake of convention, I'm using ajax_command in the function name. In the return array, command will be called from Drupal.ajax.prototype.commands scope. You can pass additional variables, and they will be in the data for JS to access (data.my_custom, data.script, likewise.)

3: Now call it!

Now that you have the JS and PHP ends, time to call the function!

$output = array();
// $output[] = ajax_command_append('#some_random_id_that_you_d_normally_use', $html);
$output[] = mymodule_ajax_command_js_eval("alert('Big Bang Theory S8!)");
$output[] = mymodule_ajax_command_console_log($stuff_you_d_normally_dpm);

print ajax_render($output);
  • 1
    Apart from using eval() that looks great - why not just pipe the data straight through to console.log?
    – Clive
    Sep 30 '14 at 12:35
  • I just updated my question. It's actually possible to create pretty and more straight forward commands. My example in particular is using eval() because I wasn't sure what commands OP wanted to run exactly. Defining the eval saved a lot time when debugging stuff :)
    – AKS
    Sep 30 '14 at 12:37
  • AyeshK All I wanted was to see some data from php variable in my ajax output, without breaking AJAX. So if you will incorporate suggestion by @Clive , your answer will be exactly what I need.
    – Mołot
    Sep 30 '14 at 12:39
  • Just added mymodule_ajax_command_console_log example.
    – AKS
    Sep 30 '14 at 12:40
  • 1
    @AyeshK i took the liberty of editing one of your strings, I'm positive you meant console_log instead of js_eval for the 2nd command
    – Clive
    Sep 30 '14 at 12:41

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.