[ACCEPTED]-jQuery: print_r() display equivalent?-debugging

Accepted answer
Score: 70

console.log is what I most often use when debugging.

I 1 was able to find this jQuery extension though.

Score: 44

You could use very easily reflection to list all properties, methods 3 and values.

For Gecko based browsers you 2 can use the .toSource() method:

var data = new Object();
data["firstname"] = "John";
data["lastname"] = "Smith";
data["age"] = 21;

alert(data.toSource()); //Will return "({firstname:"John", lastname:"Smith", age:21})"

But since 1 you use Firebug, why not just use console.log?

Score: 16

How about something like:

<script src='http://code.jquery.com/jquery-latest.js'></script>

function print_r(o){
return JSON.stringify(o,null,'\t').replace(/\n/g,'<br>').replace(/\t/g,'&nbsp;&nbsp;&nbsp;'); }

0

Score: 9

You can also do

console.log("a = %o, b = %o", a, b);

where a and b are objects.

0

Score: 6

I've made a jQuery plugin for the equivalent 1 of

<pre>
<?php echo print_r($data) ?>
</pre>

You can download it at https://github.com/tomasvanrijsse/jQuery.dump

Score: 6
$.each(myobject, function(key, element) {
    alert('key: ' + key + '\n' + 'value: ' + element);
});

This does the work for me. :)

0

Score: 5

Top comment has a broken link to the console.log 17 documentation for Firebug, so here is a link to the wiki article about Console. I 16 started using it and am quite satisfied 15 with it as an alternative to PHP's print_r().

Also 14 of note is that Firebug gives you access 13 to returned JSON objects even without you 12 manually logging them:

  • In the console you can see the url of the AJAX response.
  • Click the triangle to expand the response and see details.
  • Click the JSON tab in the details.
  • You will see the response data organized with expansion triangles.

This method take a 11 couple more clicks to get at the data but 10 doesn't require any additions in your actual 9 javascript and doesn't shift your focus 8 in Firebug out of the console (using console.log 7 creates a link to the DOM section of firebug, forcing 6 you to click back to console after).

For 5 my money I'd rather click a couple more 4 times when I want to inspect rather than 3 mess around with the log, especially since 2 keeps the console neat by not adding any 1 additional cruft.

More Related questions