Skip to main content

The Drupal for Firebug module (which can be used with Chrome or Firefox) has a firep() function built in which will log a print_r of any variable passed to it into the Drupal for Firebug widget. That might be an option.

If you really want a console log, though, I think you'd have to write the functionality yourself. It isn't too hard though, here's a quick module I knocked up that works well for me:

File: js_debug.info

name = JS Debug
core = 7.x

File: js_debug.module

function js_debug_log($var = NULL) {
  $log = &drupal_static(__FUNCTION__, array());

  if ($var) {
    $log[] = $var;
  }

  return $log;
}

function js_debug_page_alter(&$page) {
  drupal_add_js(drupal_get_path('module', 'js_debug') . '/js_debug.js', 'file');
  drupal_add_js(array('js_debug' => array('messages' => js_debug_log())), 'setting');
}

File: js_debug.js

(function($) {
  $(function() {
    if (Drupal.settings.js_debug.messages) {
      for (var i = 0; i < Drupal.settings.js_debug.messages.length; i++) {
        console.log(Drupal.settings.js_debug.messages[i]);
      }
    }
  });
})(jQuery);

The above code is obviously very bare, and doesn't check to make sure console.log() is available, etc. But it will work fine in Chrome/Firefox for any var (including arrays and objects).

To use, simply call js_debug_log($var); from code; the variable you pass will be logged in the console as you'd expect.

The Drupal for Firebug module (which can be used with Chrome or Firefox) has a firep() function built in which will log a print_r of any variable passed to it into the Drupal for Firebug widget. That might be an option.

If you really want a console log, though, I think you'd have to write the functionality yourself. It isn't too hard though, here's a quick module I knocked up that works well for me:

File: js_debug.info

name = JS Debug
core = 7.x

File: js_debug.module

function js_debug_log($var = NULL) {
  $log = &drupal_static(__FUNCTION__, array());

  if ($var) {
    $log[] = $var;
  }

  return $log;
}

function js_debug_page_alter(&$page) {
  drupal_add_js(array('js_debug' => array('messages' => js_debug_log())), 'setting');
}

File: js_debug.js

(function($) {
  $(function() {
    if (Drupal.settings.js_debug.messages) {
      for (var i = 0; i < Drupal.settings.js_debug.messages.length; i++) {
        console.log(Drupal.settings.js_debug.messages[i]);
      }
    }
  });
})(jQuery);

The above code is obviously very bare, and doesn't check to make sure console.log() is available, etc. But it will work fine in Chrome/Firefox for any var (including arrays and objects).

To use, simply call js_debug_log($var); from code; the variable you pass will be logged in the console as you'd expect.

The Drupal for Firebug module (which can be used with Chrome or Firefox) has a firep() function built in which will log a print_r of any variable passed to it into the Drupal for Firebug widget. That might be an option.

If you really want a console log, though, I think you'd have to write the functionality yourself. It isn't too hard though, here's a quick module I knocked up that works well for me:

File: js_debug.info

name = JS Debug
core = 7.x

File: js_debug.module

function js_debug_log($var = NULL) {
  $log = &drupal_static(__FUNCTION__, array());

  if ($var) {
    $log[] = $var;
  }

  return $log;
}

function js_debug_page_alter(&$page) {
  drupal_add_js(drupal_get_path('module', 'js_debug') . '/js_debug.js', 'file');
  drupal_add_js(array('js_debug' => array('messages' => js_debug_log())), 'setting');
}

File: js_debug.js

(function($) {
  $(function() {
    if (Drupal.settings.js_debug.messages) {
      for (var i = 0; i < Drupal.settings.js_debug.messages.length; i++) {
        console.log(Drupal.settings.js_debug.messages[i]);
      }
    }
  });
})(jQuery);

The above code is obviously very bare, and doesn't check to make sure console.log() is available, etc. But it will work fine in Chrome/Firefox for any var (including arrays and objects).

To use, simply call js_debug_log($var); from code; the variable you pass will be logged in the console as you'd expect.

added 43 characters in body
Source Link
Clive
  • 167.9k
  • 19
  • 303
  • 337

The Drupal for Firebug module (which can be used with Chrome or Firefox) has a firep() function built in which will log a print_r of any variable passed to it into the Drupal for Firebug widget. That might be an option.

If you really want a console log, though, I think you'd have to write the functionality yourself. It isn't too hard though, here's a quick module I knocked up that works well for me:

File: js_debug.info

name = JS Debug
core = 7.x

File: js_debug.module

function js_debug_log($var = NULL) {
  $log = &drupal_static(__FUNCTION__, array());

  if ($var) {
    $log[] = $var;
  }

  return $log;
}

function js_debug_page_alter(&$page) {
  drupal_add_js(array('js_debug' => array('messages' => js_debug_log())), 'setting');
}

File: js_debug.js

(function($) {
  $(function() {
    if (Drupal.settings.js_debug.messages) {
      for (var i = 0; i < Drupal.settings.js_debug.messages.length; i++) {
        console.log(Drupal.settings.js_debug.messages[i]);
      }
    }
  });
})(jQuery);

The above code is obviously very bare, and doesn't check to make sure console.log() is available, etc. But it will work fine in Chrome/Firefox for any var (including arrays and objects).

To use, simply call js_debug_log($var); from code; the variable you pass will be logged in the console as you'd expect.

The Drupal for Firebug module (which can be used with Chrome or Firefox) has a firep() function built in which will log a print_r of any variable passed to it into the Drupal for Firebug widget. That might be an option.

If you really want a console log, though, I think you'd have to write the functionality yourself. It isn't too hard though, here's a quick module I knocked up that works well for me:

File: js_debug.info

name = JS Debug
core = 7.x

File: js_debug.module

function js_debug_log($var = NULL) {
  $log = &drupal_static(__FUNCTION__, array());

  if ($var) {
    $log[] = $var;
  }

  return $log;
}

function js_debug_page_alter(&$page) {
  drupal_add_js(array('js_debug' => array('messages' => js_debug_log())), 'setting');
}

File: js_debug.js

(function($) {
  $(function() {
    if (Drupal.settings.js_debug.messages) {
      for (var i = 0; i < Drupal.settings.js_debug.messages.length; i++) {
        console.log(Drupal.settings.js_debug.messages[i]);
      }
    }
  });
})(jQuery);

The above code is obviously very bare, and doesn't check to make sure console.log() is available, etc. But it will work fine in Chrome/Firefox.

To use, simply call js_debug_log($var); from code; the variable you pass will be logged in the console as you'd expect.

The Drupal for Firebug module (which can be used with Chrome or Firefox) has a firep() function built in which will log a print_r of any variable passed to it into the Drupal for Firebug widget. That might be an option.

If you really want a console log, though, I think you'd have to write the functionality yourself. It isn't too hard though, here's a quick module I knocked up that works well for me:

File: js_debug.info

name = JS Debug
core = 7.x

File: js_debug.module

function js_debug_log($var = NULL) {
  $log = &drupal_static(__FUNCTION__, array());

  if ($var) {
    $log[] = $var;
  }

  return $log;
}

function js_debug_page_alter(&$page) {
  drupal_add_js(array('js_debug' => array('messages' => js_debug_log())), 'setting');
}

File: js_debug.js

(function($) {
  $(function() {
    if (Drupal.settings.js_debug.messages) {
      for (var i = 0; i < Drupal.settings.js_debug.messages.length; i++) {
        console.log(Drupal.settings.js_debug.messages[i]);
      }
    }
  });
})(jQuery);

The above code is obviously very bare, and doesn't check to make sure console.log() is available, etc. But it will work fine in Chrome/Firefox for any var (including arrays and objects).

To use, simply call js_debug_log($var); from code; the variable you pass will be logged in the console as you'd expect.

added 964 characters in body
Source Link
Clive
  • 167.9k
  • 19
  • 303
  • 337

The Drupal for Firebug module (which can be used with Chrome or Firefox) has a firep() function built in which will log a print_r of any variable passed to it into the Drupal for Firebug widget. That might be an option.

If you really want a console log, though, I think you'd have to write the functionality yourself. It shouldn't beisn't too hard though. In fact I'll knock up, here's a quick module I knocked up that works well for me:

File: js_debug.info

name = JS Debug
core = 7.x

File: js_debug.module

function js_debug_log($var = NULL) {
  $log = &drupal_static(__FUNCTION__, array());

  if ($var) {
    $log[] = $var;
  }

  return $log;
}

function js_debug_page_alter(&$page) {
  drupal_add_js(array('js_debug' => array('messages' => js_debug_log())), 'setting');
}

File: js_debug.js

(function($) {
  $(function() {
    if (Drupal.settings.js_debug.messages) {
      for (var i = 0; i < Drupal.settings.js_debug.messages.length; i++) {
        console.log(Drupal.settings.js_debug.messages[i]);
      }
    }
  });
})(jQuery);

The above code is obviously very bare, and doesn't check to domake sure console.log() is available, etc. But it and postwill work fine in Chrome/Firefox.

To use, simply call js_debug_log($var); from code; the code back herevariable you pass will be logged in a bitthe console as you'd expect.

The Drupal for Firebug module (which can be used with Chrome or Firefox) has a firep() function built in which will log a print_r of any variable passed to it into the Drupal for Firebug widget. That might be an option.

If you really want a console log, though, I think you'd have to write the functionality yourself. It shouldn't be too hard though. In fact I'll knock up a quick module to do it and post the code back here in a bit.

The Drupal for Firebug module (which can be used with Chrome or Firefox) has a firep() function built in which will log a print_r of any variable passed to it into the Drupal for Firebug widget. That might be an option.

If you really want a console log, though, I think you'd have to write the functionality yourself. It isn't too hard though, here's a quick module I knocked up that works well for me:

File: js_debug.info

name = JS Debug
core = 7.x

File: js_debug.module

function js_debug_log($var = NULL) {
  $log = &drupal_static(__FUNCTION__, array());

  if ($var) {
    $log[] = $var;
  }

  return $log;
}

function js_debug_page_alter(&$page) {
  drupal_add_js(array('js_debug' => array('messages' => js_debug_log())), 'setting');
}

File: js_debug.js

(function($) {
  $(function() {
    if (Drupal.settings.js_debug.messages) {
      for (var i = 0; i < Drupal.settings.js_debug.messages.length; i++) {
        console.log(Drupal.settings.js_debug.messages[i]);
      }
    }
  });
})(jQuery);

The above code is obviously very bare, and doesn't check to make sure console.log() is available, etc. But it will work fine in Chrome/Firefox.

To use, simply call js_debug_log($var); from code; the variable you pass will be logged in the console as you'd expect.

Source Link
Clive
  • 167.9k
  • 19
  • 303
  • 337
Loading