Home » Javascript

print_r in Javascript

8 January 2010 3,982 views Popularity: 8% Share/Bookmark

email

It’s a real headache when you have to work on objects and arrays in Javascript. It would be lot easier to detect elements of the Javascript objects/arrays if we have print_r function in Javascript as we have in PHP.

I googled the web and have found a very efficient print_r Javascript function. This has helped me a lot in my projects. Here is the code:-

<script type="text/javascript">
	function print_r(theObj){
	   if(theObj.constructor == Array || theObj.constructor == Object){
		  document.write("<ul>")
		  for(var p in theObj){
			 if(theObj[p].constructor == Array || theObj[p].constructor == Object){
				document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
				document.write("<ul>")
				print_r(theObj[p]);
				document.write("</ul>")
			 } else {
				document.write("<li>["+p+"] => "+theObj[p]+"</li>");
			 }
		  }
		  document.write("</ul>")
	   }
	}
</script>

USING PRINT_R

<script type="text/javascript">
print_r(JAVACRIPT_ARRAY_OR_OBJECT);
</script>

Source: http://www.brandnewbox.co.uk/articles/details/a_print_r_equivalent_for_javascript/

Related posts:

  1. Javascript: Show/Hide HTML elements
  2. Website statistic (User Information) in Javascript
  3. Javascript: Add Remove HTML elements
  4. Javascript: How to Submit form and Change form action?
  5. PHP Javascript : Playing with multi-dimensional array
  6. jQuery: Print array and object
  7. jQuery: Grey out background and preview image as popup
  8. Javascript: How to get current URL without Query string?
  9. Go back link in Javascript
  10. Javascript: Show Hide textbox text on focus
  • http://www.javascriptbank.com/javascript-countdown-timer.html Javascript Countdown Timer

    very cool & good code, thank you very much for sharing.

    Can you contribute this code on my JavaScript library?

    Awaiting your response. Thank

  • http://blog.chapagain.com.np Mukesh

    The source of this js code is from another site. I mentioned the link in the post. So, you can post to your js bank yourself.

  • Khalid Chauhdry

    Thanks Mukesh Chapagain it’s a good function

  • http://www.facebook.com/phat.reaction Andrew Killen

    Mukesh,

    I found when I setup your function if did not correctly place the UL elements around sub arrays in a multidimensional array.  

    So instead (used a bit of jquery to append write the output, but could just as easily be inner) I chose to add an extra function variable of return_value that will make the response be returned rather than echoed.  this way I was able to build the entire list before outputting…. 

    function print_r(theObj, return_value){
     var html = “”;
           if(theObj.constructor == Array || theObj.constructor == Object){
              for(var p in theObj){
                 if(theObj[p].constructor == Array || theObj[p].constructor == Object){
                 html += “["+p+"] => “+typeof(theObj);
                 html +=  print_r(theObj[p], true);
                 html += “”;                
                 } else {
                  html += “["+p+"] => “+theObj[p]+”";
                 }
              }
           }
           html += “”;
           if(return_value ==  undefined){
          $(‘#debug’).append(html);
          }
          else{
          return html;
          }
        }