Tip: Click lines to highlight, hold ctrl/cmd to multi-select

simple JavaScript logger (1-Dec @ 10:04)

logg method that prints the log statements on the page

www.flickr.com-photos-bjarlestam

Syntax Highlighted Code

  1. <span id='testOutput'>Ouptput: <br/></span>
  2.  
  3.  
  4. <script language="JavaScript" type="text/javascript">
  5.  
  6. function logg(text) {
  7.     var output = document.getElementById('testOutput');
  8.     output.innerHTML=output.innerHTML + text + "<br></br>";
  9.     return true;
  10. }
  11.  
  12. //Not sure this one works correctly
  13. function dumpProps(obj, parent) {
  14.    // Go through all the properties of the passed-in object
  15.    for (var i in obj) {
  16.       // if a parent (2nd parameter) was passed in, then use that to
  17.       // build the message. Message includes i (the object's property name)
  18.       // then the object's property value on a new line
  19.       if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
  20.       // Display the message. If the user clicks "OK", then continue. If they
  21.       // click "CANCEL" then quit this level of recursion
  22.       if (!logg(msg)) { return; }
  23.       // If this property (i) is an object, then recursively process the object
  24.       if (typeof obj[i] == "object") {
  25.          if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
  26.       }
  27.    }
  28. }
  29. </script>

Plain Code

<span id='testOutput'>Ouptput: <br/></span>


<script language="JavaScript" type="text/javascript">

function logg(text) {
    var output = document.getElementById('testOutput');
    output.innerHTML=output.innerHTML + text + "<br></br>";
    return true;
}

//Not sure this one works correctly
function dumpProps(obj, parent) {
   // Go through all the properties of the passed-in object 
   for (var i in obj) {
      // if a parent (2nd parameter) was passed in, then use that to 
      // build the message. Message includes i (the object's property name) 
      // then the object's property value on a new line 
      if (parent) { var msg = parent + "." + i + "\n" + obj[i]; } else { var msg = i + "\n" + obj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they 
      // click "CANCEL" then quit this level of recursion 
      if (!logg(msg)) { return; }
      // If this property (i) is an object, then recursively process the object 
      if (typeof obj[i] == "object") { 
         if (parent) { dumpProps(obj[i], parent + "." + i); } else { dumpProps(obj[i], i); }
      }
   }
}
</script>

Codedump Run

Permalink: http://codedumper.com/simple-javascript-logger