View Full Version : Object debugger

11-03-2003, 10:13 PM
This is a script that frequently keeps me from using bad words.

It loops through an object and returns its properties in text (for alert) or html format.


var obj = {id:3,arr:[2,3,"str",{prop1:2,prop2:""}],nr:3}


pValue: The object
pName: Instance name of the object. Default: "input"
pType: Which type to be return (text or html). Default: "text"
pLevels: The max amount of levels. Useful if an object references a parent. Default: infinite

A string with the properties

function _viewObj(pValue,pName,pType,pLevels,pObj) {

function objExists(pCurr) {
for (var i=0; i<pObj.length; i++) {
if (pObj[i]==pCurr) {
return true;
return false;

pType = new String(pType).toLowerCase();
if (new String(pName) == "undefined")
pName = "input";
if (new String(pType).search(/html|text/i) == -1)
pType = "text";
if (pLevels == "undefined")
pLevels = -1; /* Infinite */
if (typeof pObj == "undefined")
pObj = [];
var str="";
if (typeof pValue != "function") { // No functions / methods - just values
var br=(pType == "text"? "\n" : (pType == "html" ? "<br>" : ""));
if ((typeof pValue == "object" && !!pValue) && (pValue.length) && (!pValue.substring)) { // An array
try {
for (var i=0;i<pValue.length;i++) {
if (pLevels==0) {
str+=br+pName+"["+i+"]: "+pValue;
str+=""+_viewObj(pValue[ i ],pName+"["+i+"]",pType,pLevels-1,pObj);
} catch (e) {
str+=br+pName+": "+pValue+" (Not an array)";
} else if (typeof pValue == "object" && !!pValue && (!pValue.substring)){ // An object
try {
for (var prop in pValue) {
if (pLevels==0) {
str+=br+pName+"."+prop+": "+pValue;
if (typeof pValue[prop] == "object" && objExists(pValue[prop])) {
str+=br+pName+"."+prop+": [Parent Object]";
} catch (e) {
str+=br+pName+": "+pValue+" (Not an object)";
} else{// if (typeof pValue != "object") { // Simple value
str+=br+pName+": "+pValue;
return str;

Avoiding never-ending loops by checking for parent objects.

var parentObj={id: 2};
var test={id: 1,par: parentObj};

11-04-2003, 02:27 PM
Looks pretty good.

Mine (http://www.codingforums.com/showthread.php?s=&threadid=20663) has a hardcoded limit of five, due to IE crashing on more than the simplest nested objects.

11-04-2003, 04:56 PM
Yes, it seems your has a better support for DOM elements, by avoiding some properties.

IE takes forever when I tried to run a <td> element in 2 levels on mine. Mozilla had no problem with that, but crashed on 3 levels.

11-04-2003, 08:10 PM
Huh? I made one that built the entire tree from window up in moz, making sure to eliminate infinite loops by cheching whether an object has been expanded before, and if so just referring to the first instance. It didn't break. Ie crashed because of some non-JScript objects, but op5 (which was the newest opera at that time) handled it just fine.

11-04-2003, 09:06 PM
i'd like to see that, if i could.

11-05-2003, 11:50 AM
Good suggestion liorean!

I added a check for parent objects. Unfortunately it didn't work with DOM anyway. If you can see what's wrong, you are most welcome to share that information...:)

11-05-2003, 12:40 PM
'll see if I can find it in my archives. That was on my previous machine...