Created
December 6, 2011 16:13
-
-
Save wilsonpage/1438757 to your computer and use it in GitHub Desktop.
print_r() for javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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+=' ' }; | |
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