Javascript Debug Window Script CodeUnit 07 JUN 2010

I found this little snippet of code floating around the Internet and despite its age, it still proves to be quite a handy little guy to have around when you are looking to quickly pop in a debug window in order to test some values against when in the middle of debugging some of that existing code of yours.

The premise for the script is pretty simple. Create a popup window and direct all debug comments into the new “debug” window, meaning that your site display remains clean and tidy but at the same time you’re are getting all the hidden variable values you really care about.

So no more print_r and annoying alerts to break the flow of your web application in other words! :P

// Show the debug window
function showDebug() { ="",
                  +"status=yes,resizable=yes"); = self;
  // open the document for writing;
      "Debug Window

// If the debug window exists, then write to it
function debug(text) {
  if ( && ! {"n");

// If the debug window exists, then close it
function hideDebug() {
  if ( && ! {; = null;

To use is pretty simple really. At the start of your page, generate the debug window by calling showDebug(). Then, whenever you want to print something out, simply make use of the debug function, a little like this: debug(“this is a debug message”). Finally, if you are a purist, you may as well close your popup debug window by using hideDebug().

Of course, today’s awesome tools like Firefox’s plugin extraordinaire Firebug and even Internet Explorer’s built in debugger makes debugging Javascript pretty simple, but still, it remains pretty useful when you want to do something yourself and that little something you want to check doesn’t quite fall under the auspices of the existing tools at hand! :)

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.

  • drb

    nice post, thanks…
    however, it works for me with Firefox and IE 8, but debugWindow is always blank with Google Chrome