Introduction
This is a useful tool whenever you work with JavaScript and the compatibility issues between navigators.
You can inspect any JavaScript objects and list them as indented, ordered by levels.
It shows you type and property name. If an object property can't be accessed, an error message will be shown.
Using the Code
function inspect(obj [, maxLevels [, level]])
Input Vars
obj
: Object to inspectmaxLevels
: Optional. Number of levels you will inspect inside the object. Default MaxLevels
=1level
: RESERVED for internal use of the function
Return Value
HTML formatted string
containing all values of inspected object obj
.
function inspect(obj, maxLevels, level)
{
var str = '', type, msg;
if(level == null) level = 0;
if(maxLevels == null) maxLevels = 1;
if(maxLevels < 1)
return '<font color="red">Error: Levels number must be > 0</font>';
if(obj == null)
return '<font color="red">Error: Object <b>NULL</b></font>';
str += '<ul>';
for(property in obj)
{
try
{
type = typeof(obj[property]);
str += '<li>(' + type + ') ' + property +
( (obj[property]==null)?(': <b>null</b>'):('')) + '</li>';
if((type == 'object') && (obj[property] != null) && (level+1 < maxLevels))
str += inspect(obj[property], maxLevels, level+1);
}
catch(err)
{
if(typeof(err) == 'string') msg = err;
else if(err.message) msg = err.message;
else if(err.description) msg = err.description;
else msg = 'Unknown';
str += '<li><font color="red">(Error) ' + property + ': ' + msg +'</font></li>';
}
}
str += '</ul>';
return str;
}
History
- 22nd March, 2008: Article posted
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.