Debug - Your own console

R

Robert

Simple way of writing debug print statements in your JavaScript code.
You need to have enabled popups in your browser.

I liked the ability to write to the java console in Netscape 4.x.
This, ability is missing in IE and the latest versions of Netscape.
Maybe someone knows a better way and can improve on this code, the
following JavaScript code demonstrates how to write debug statements
in a JavaScript program.

I sure lots of people know how, but some probably do not.

Robert

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Example Debug Console</TITLE>

<SCRIPT LANGUAGE="JavaScript">

// Global variable for debug window. Function startDebug must be
// called before first call to the Out function.
var debugMode = "yes";
var newWindow = "";
var debugWindowName = "debugconsole"


// Debug functions

function startDebug()
{
//Create the debug window. Popups must be enabled in browser.
var aDate = new Date();
if (debugMode == "yes")
{

newWindow = newWindow = window.open(
"",debugWindowName,"scrollbars=yes,resizable=yes,width=700,height=500");
newWindow.document.writeln(
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">');
newWindow.document.writeln("<html>");
newWindow.document.writeln(
"<header><title>Window " + debugWindowName + "</title></header>");
newWindow.document.writeln(
"<body><p>The debug information that follows was generated on "
+ aDate + "</p>");
}

}

// Debug functions
function DumpProperties(obj, obj_name)
{
// Example invocation: DumpProperties(document,'document');
if (debugMode == "yes" )
{
Out('In DumpProperties. obj=' + obj_name);
for (i in obj)
{
//Some versions of IE create an invalid reference which the
//catch statement suppresses. Note: Early versions of
//JavaScript do not suppport the try statement,
//so you will need to get rid of it. Without the try
//statement in IE, you may need to avoid using this
//function.
try
{
Out(obj_name + "." + i + " = " + obj);
}
catch(e)
{
Out("Error writing out structure. Was " + e + " for " + i);
}
}
}
}

function Out(obj)
{

if (debugMode == "yes" )
{

// Put to debug window
var newString = "";

for (i=0;i<obj.length;i++)
{

if (obj.substr(i,1) == "<")
newString += "&lt;";
else if (obj.substr(i,1) == ">")
newString += "&gt;";
else if (obj.substr(i,1) == "&")
newString += "&amp;";
else
newString += obj.substr(i,1);
}

newWindow.document.writeln(newString + "<br>");

}
return;
}
</script>
</HEAD>

<BODY>
<SCRIPT LANGUAGE="JavaScript">

//Example of deciding at run time whether or not to invoke debuging.
/* But not today.
if ( location.search.length >= 10
&& location.search.substr(0,10) == "?debug=yes" )
{
debugMode = "yes";
}
*/

//Initialize debug
startDebug();

Out(" ---------------------------------------- ");


Out("Now try location");
DumpProperties(location,'location');

Out("Let's see if we can dump out the whole document object");

DumpProperties(document,'document');

</script>

<P>For those folks with popup windows enabled, you should see a
popup window
containing a display of the location and document objects.</p>

<p>The idea and the general JavaScript implementaion came from the
book
<b>Visual QuickStart Guide: JavaScript for the World Wide Web</b>, 2
nd Edition by Tom Negrino
and Dori Smith.</p>

</BODY></HTML>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top