Wednesday, May 18, 2011

printObj() => outputs object properties

The javascript function below is used to output the objects and its properties. Alternative to the php function => print_r
I use it mostly for looking up the document DOM eg. printObj(document)

Insert code inside JavaScript <script> tags
_________________________________________________

function printObj(obj) //reads the objects properties
{
var object ="";// sets the variable
for(var type in obj)
{object += type +"=> " + obj[type] + "<br /> ";}
document.write(object);
}

//create a new array or object
var num = new Array(1,2,3,45,6);
//outpuy the object and its properties by
printObj(num)
______________________________________________

Output:
0=> 1
1=> 2
2=> 3
3=> 45
4=> 6



bookmarklet: shows all the the p tags for the page. Copy and paste the code below in the address bar.

javascript:e = document.getElementsByTagName('p');function printObj(obj){var object ="";for(var type in obj){object += type +"=> " + e[type].innerHTML + "<br />";}document.write(object);}printObj(e);

No comments:

Post a Comment