[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Is there a way to see object contents using Javascript?

Posted on 2009-02-13
5
Medium Priority
?
2,113 Views
Last Modified: 2012-05-06
Hello everybody!

I am using javascript and I debugging a script.  I am using Firebug to debug it.  Whenever I output an object it say something like [object Object].  Is there an easy way to output the contents of an object.  So I can see the properties and methods in the form of a string or whatever it contains?

Thanks!
0
Comment
Question by:parlays
5 Comments
 
LVL 16

Expert Comment

by:anoyes
ID: 23638739
You can loop thru the properties of an object using a for loop:
for (var i in object)
 {
  alert(i);
 }

Is that what you're looking for?
0
 
LVL 15

Assisted Solution

by:fsze88
fsze88 earned 800 total points
ID: 23639126
in FF3 by alert object, it should able to show what type of object/element is...
But also, you could got the properties by obj.nodeName, obj.type, obj.value .........
please visit https://developer.mozilla.org/En/DOM/Node , it would show how to get attributes from an object/element
please try code snippet
<script language="javascript">
function alertme(obj){
alert(obj);
alert("obj.nodeName : " + obj.nodeName + " obj.type : " + obj.type + " obj.value : " + obj.value + " obj.nodeType : " + obj.nodeType + " obj.attributes['privatevalue'].value : " +obj.attributes['privatevalue'].value );
 
}
</script>
 
 
<form name="form1">
 
<input type="button" privatevalue="testing" onclick="alertme(this);" value="check this element properties">
</form>

Open in new window

0
 
LVL 6

Expert Comment

by:havj123
ID: 23640022
tyy alert(objectname);
0
 
LVL 41

Accepted Solution

by:
HonorGod earned 1200 total points
ID: 23640510
Yes.  

Put the attached file someplace convenient (e.g., in the same directory as your other JavaScript code, or in the same folder as your HTML).

Add a <script ...> tag to include the code

Add a <div id='debug'></div> to the end of your page

and then add code like shown below to display the contents of the specified object ("obj" in the example).

If you only want the "string" attributes display, add a 4 as the 3rd, optional parameter:

There is an optional 3rd parameter that defines what kind of attributes should be displayed.  This "flags" parameter is a combination of the following bit fields:

Mask:  Binary    Dec   typeof()                              
      --------   ---   --------                              
      00000001 =   1 = boolean                              
      00000010 =   2 = number                                
      00000100 =   4 = string                                
      00001000 =   8 = function                              
      00010000 =  16 = object                                
      00100000 =  32 = other                                
      01000000 =  64 = constants     name matches /^[A-Z_]+$/
      10000000 = 128 = TBD                                  

So, to display "strings" and "functions" just add 4 + 8 = 12, and have this value (12) as the 3rd parameter.

Does this make sense?
<script type='text/javascript' src='objDisplay.js'></script>
 
...
  var debug = document.getElementById( 'debug' )
  if ( debug ) {
      // If the debug "div" exists, display the entire object contents
    debug.innerHTML = objDisplay( 'objectName', obj )
  }
 
  // if you only want to display the string attributes, the call would be:
  debug.innerHTML = objDisplay( 'objectName', obj, 4 )

Open in new window

objDisplay.js.txt
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 23891719
Thanks for the grade & points.

Good luck & have a great day.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question