ManticMoo.COM All Articles Jeff's Articles
Jeffrey P. Bigham

A Virtual Javascript Console for Debugging

Jeffrey P. Bigham

Related Ads

Sometimes when developing applications with Javascript it's useful to have a place to output commands. You could do that using alerts, but that's pretty annoying. And you could even hook it up with the Javascript Console provided by Firefox, but then you mix your output with errors or other browser messages. This simple little Javascript code, adds a virtual Javascript console that you can write to. It's quite simple (just look at it), but it can be extremely useful.

To use it, just include the following code in with your Javascript:


function writeConsole(content) {
  if(!(top.consoleRef)) {
    top.consoleRef=window.open('','myconsole',
      'width=350,height=250'
      +',menubar=0'
      +',toolbar=1'
      +',status=0'
      +',scrollbars=1'
      +',resizable=1')
   top.consoleRef.document.writeln(
    '<html><head><title>Console</title></head>'
    +'<body bgcolor=white onLoad="self.focus()"><h>Console</h1>'
    + '</body></html>'
  )
  }

  top.consoleRef.document.body.innerHTML += content + "\n";
}

When you want to write something to the console, call it like so:


writeConsole('A message that will be written on the console.');

Jeffrey P. Bigham
ManticMoo.COM All Articles Jeff's Articles