Javascript: write Console.debug () output to browser?

I need to be able to accept any JSON data and print key / value pairs.

(something similar to print_r () in PHP)

Is this possible with javascript?

+6
json javascript loops
source share
6 answers

Yes, you can process an amazing amount of information through an alert, and you can also use it for debugging.

Here is also the print_r equivalent for 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>") } } 

good luck with your project!

+5
source share

Usually I just quickly create a log function that allows you to change the logging method. Record enablers / disablers or comment to select options.

 function log(msg){ if (window.console && console.log) { console.log(msg); //for firebug } document.write(msg); //write to screen $("#logBox").append(msg); //log to container } 

Update: Firebug Console API Information

Update: Added check for browsers without firebug.

+13
source share

I would recommend you get a JSON syntax library, for example JSON2 , because it can "reinforce" your objects, then you can simply:

 var myString = JSON.stringify(myObject); 

myString will now contain a string representation of myObject .

But if this is for debugging purposes, I would recommend you get a JavaScript debugger like Firebug , you will get a lot of useful features in the Console API .

+2
source share

can you just use the following:

  document.write('<h2>Your Text and or HTML here.</h2>'); 
+1
source share

FireBug is a great tool! Indispensable! I found this eliminates the need to write debugging data to my pages, and I can view JSON all day.

0
source share

I need to be able to take any JSON data and print key / value pairs.

Then print the JSON data. JSON is a designation, not an object. If you have JSON data, you already have everything you need. If you want this to be a little more bizarre, you can add a line after each "\s*,

If you want to deconstruct an object, this is not possible if you are not using JavaScript, since ECMAScript cannot create circular references in a single object literal. If it's JavaScript-only, you can use uneval(object) , which will use harsh variables. (for example, ({x:#1={y:#1#}}) ).

0
source share

All Articles