6.23.08 Javascript print_r function

There are many versions of this file floating around - but I had to whip one together I use in almost all my development and unit/regression testing facilities. This will output a clean hierarchical div-based display of any object - at least within the browser’s recursive traversal limitations.

And here’s some output of a JSON string

print_r([{function:‘testing’,arguments:[‘arg1′, ‘arg2′]}])

[0] => object
[function] => testing
[arguments] => object
[0] => arg1
[1] => arg2

Here’s the function:

function print_r(theObj) {
    var retStr = '';
    if (typeof theObj == 'object') {
        retStr += '<div style="font-family:Tahoma; font-size:7pt;">';
        for (var p in theObj) {
            if (typeof theObj[p] == 'object') {
                retStr += '<div><b>['+p+'] => ' + typeof(theObj) + '</b></div>';
                retStr += '<div style="padding-left:25px;">' + print_r(theObj[p]) + '</div>';
            } else {
                retStr += '<div>['+p+'] => <b>' + theObj[p] + '</b></div>';
            }
        }
        retStr += '</div>';
    }
    return retStr;
}

Download this code: print_r.js


No comments




0.284s