Skip to content

Instantly share code, notes, and snippets.

@wilsonpage
Created December 6, 2011 16:13
Show Gist options
  • Save wilsonpage/1438757 to your computer and use it in GitHub Desktop.
Save wilsonpage/1438757 to your computer and use it in GitHub Desktop.
print_r() for javascript
(function($){
$.print_r = (function(){
var depth = 0;
var render = function(o){
var str='',
indent = (function(){
var res="";
for(var i=0; i<depth*4; i++){ res+='&nbsp;' };
return res;
})();
for(var p in o){
if(typeof o[p] == 'string'){
str+= indent + p + ': ' + o[p]+' </br>';
}else{
// increase the depth
depth++;
// run the render function on this object
str+= indent + p + ': { </br>' + render(o[p]) + indent + '}<br>';
depth--;
}
}
// return the string
return str;
};
return function(o, options){
options = options || {};
//[Defaults]
var autoAppend = options.hasOwnProperty('autoAppend') ? options.autoAppend : true;
// create string
var str = render(o);
// append to body unless staed otherwise
if(autoAppend) $('body').append(str);
// return the str too
return str;
};
})();
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment