/ Published in: JavaScript
In javascript there is no function for printing variables like you have PHP, I use print_r and var_dump a lot in PHP for dumping variables
so I wanted to create a simular function in javascript.
so I wanted to create a simular function in javascript.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function var_dump( objElement, intLimit, intDepth ) { intDepth = intDepth?intDepth:0; intLimit = intLimit?intLimit:1; strReturn = '<ol>'; for( property in objElement ) { // Property domConfig isn't accesible if( property != 'domConfig' ) { strReturn += '<li><strong>' + property + '</strong> <small>(' + ( typeof objElement[property] ) + ')</small>'; if ( typeof objElement[property] == 'number' || typeof objElement[property] == 'boolean' ) { strReturn += ' : <em>' + objElement[property] + '</em>'; } if ( typeof objElement[property] == 'string' && objElement[property] ) { strReturn += ': <div style="background:#C9C9C9; border:1px solid black; overflow:auto;"><code>' + objElement[property].replace(/</g, '<').replace(/>/g, '>') + '</code></div>'; } if ( typeof objElement[property] == 'object' && ( intDepth < intLimit ) ) { strReturn += var_dump( objElement[property], intLimit, ( intDepth + 1 ) ); } strReturn += '</li>'; } } strReturn += '</ol>'; if ( intDepth == 0 ) { winpop = window.open( "", "", "width=800, height=600, scrollbars, resizable" ); winpop.document.write( '<pre>' + strReturn + '</pre>' ); winpop.document.close(); } return strReturn; }
URL: http://remorse.nl/2008/07/javascript_debugging_print_rvar_dump_in_javascript_like_php/