Monday, April 14, 2008

Dead-simple portable JavaScript logger

window.console = {
  log : function (message) {
    if ($('#logpane').length == 0)
      $('body').append("<ul id='logpane'></ul>");
    $("#logpane").append($("<li></li>").text(message));
  }
};

This requires jQuery, and should work on any browser and on most pages.

It intentionally uses the name 'console.log', so it overrides logging to Firebug's logger.

To use it:

<script type="text/javascript">
console.log("foo");
console.log("bar");
<script>

It's not very pretty - just appends a list of log messages at the bottom:

  • foo
  • bar