Open Source Content Management System

Planet Midgard: Archive

2006-09-30 - 2006-10-30

Javascript debugging

Posted on 2006-10-30 21:07:40 GMT.

My last post was of CRIR (Checkbox Radio Input Replacement), but after some testing, it quite does not work good enough. There is the"small" matter of Safari not handling labels as it should etc. I finally got my CRIR working, and it's been tested on MAC [Safari, FF, Opera] and on Windows [IE, FF, Opera]. Works like a charm.

One big thing was that there is no universal Javascript debugging tools. FF has Firebug, Safari has its own way with debugging, Opera does it like this and then we always have the alert -clicking if no debug-log viewers are handy. I made a small debugger today, hope it helps someone. It requires the Prorotype javascript library.

To use it you simply call:
debugHelper("Some informative text", [element or variable] , [what level of log this belongs to]);

example on firefox:

<input type="radio" name="radio_btn" id="radio1" />

debugHelper( "The input element we are working on: ", $(radio1), 1 );

this will give us:

The input element we are working on: <input type="radio" name="radio_btn" id="radio1" />

The code:

/**
* Testing script for error output in different browsers.
* - FF => console.log()
* - IE => alert
* - safari => window.console.log()
* - Opera => opera.postError()
* @param {String} txt Explaining text for this
* @param {Object} info Value of error (Object, Array, number,...)
* @param {Object} level What elements to show, ALL = every element
*/
function debugHelper ( txt, info, level ) {

if ( debuggingMode ) {

var opt = -1;

if (level != "ALL") {
opt = debuggingLevel.indexOf(level);
}

if ( opt != -1 || level == "ALL" ) {
switch (BrowserDetect.browser) {
case "Firefox":
return console.log(txt, info);
break;
case "Safari":
return window.console.log(txt+info);
break;
case "Opera":
return opera.postError(txt+info);
default:
return alert(txt+info);
}
}
}
}

// debug
var debuggingMode = true;
var debuggingLevel = $A( new Array(5,6) );
Designed by Nemein, hosted by Anykey