Introduce DataBase,Asp.net,JavaScript,Xml,Html,Css,Sql,Php,ASP.NET Controls,AJAX,Tools,HTML,CSS,JavaScript,Open Source Project,WPF,.Net Framework,Linq
Top Recommended Hosting

How to Inspect a JavaScript Object

by the3factory 3/23/2008 5:56:00 AM

A useful tool whenever you work with Javascript and the compatibility issues between navigators.
You can inspect any Javascript Object and list them 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 inspect
maxLevels
Optional. Number of levels you will inspect inside the object.
Default MaxLevels=1
level
RESERVED for internal use of the function.

Return Value:
HTML formated string contaning all values of inspected object "obj".

Collapse
function inspect(obj, maxLevels, level)
{
var str = '', type, msg;
  // Start Input Validations
// Dont touch, we start iterating at level zero
    if(level == null)  level = 0;
    // At least you want to show the first level
if(maxLevels == null) maxLevels = 1;
if(maxLevels < 1)     return '<font color="red">Error: Levels number must be > 0</font>';
    // We start with a non null object
if(obj == null)
return '<font color="red">Error: Object <b>NULL</b></font>';
    // End Input Validations
  // Each Iteration must be indented
str += '<ul>';
  // Start iterations for all objects in obj
for(property in obj)
{
try
{
        // Show "property" and "type property"
type =  typeof(obj[property]);
str += '<li>(' + type + ') ' + property + ( (obj[property]==null)?(': <b>null</b>'):('')) + '</li>';
        // We keep iterating if this property is an Object, non null
// and we are inside the required number of levels
          if((type == 'object') && (obj[property] != null) && (level+1 < maxLevels))
str += inspect(obj[property], maxLevels, level+1);
}
catch(err)
{
      // Is there some properties in obj we can't access? Print it red.
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>';
}
}
    // Close indent
str += '</ul>';
return str;
}
 

Related posts

Sign up for PayPal and start accepting credit card payments instantly.


Powered by BlogEngine.NET 1.2.0.0