Click here to Skip to main content
Email Password   helpLost your password?

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

Return Value

HTML formatted string containing all values of inspected object obj.

function inspect(obj, maxLevels, level)
{
  var str = '', type, msg;

    // Start Input Validations
    // Don't 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;
}

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralNot bad ...
Vertyg0
1:39 22 Mar '08  
But I like firebug (firefox extension) more ...
Cheers!
GeneralRe: Not bad ...
Ariel Tapia
14:49 17 Jun '08  
I agree with you, but it just works on 1 browser.
Smile
GeneralRe: Not bad ... [modified]
nickyt
4:52 22 Jul '08  
FireBug does only work on FireFox, but the latest Opera version has a debugger for JavaScript built-in, see http://www.opera.com/products/dragonfly/[^] and Safari has I believe at a minimum a JavaScript console, see my blog entry on how to configure it, http://www.rugbyincanada.com/webmaster/2008/04/debugging-tools-for-web-applications.aspx[^]. And for Internet Explorer you have the debugger available with Visual Studio.NET (2003/2005/2008). As well there is the add on for Internet Explorer (for versions 6 to 8) called the IE Developer Toolbar, see http://www.microsoft.com/downloadS/details.aspx?familyid=E59C3964-672D-4511-BB3E-2D5E1DB91038&displaylang=en[^].

The code you wrote is fun, but trivial. Stuff like recursive properties could not be handled. You should actually take a peek under the FireBug hood to see how all their magic happens. I haven't, but I imagine there are some great JavaScript nuggets in there.

Cheers,
nickyt


modified on Saturday, November 8, 2008 11:39 PM

General:)
Ariel Tapia
17:11 8 Nov '08  
Thanks.


Last Updated 22 Mar 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010