469,352 Members | 1,661 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,352 developers. It's quick & easy.

Debug - Your own console

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,w idth=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[i]);
}
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>
Jul 20 '05 #1
1 1884
Ops, left out declare of variable i in two of the functions.

Robert
Corrected code:
<!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,w idth=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)
{
var i;
// 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[i]);
}
catch(e)
{
Out("Error writing out structure. Was " + e + " for " + i);
}
}
}
}

function Out(obj)
{

if (debugMode == "yes" )
{

// Put to debug window
var i;
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>
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Nick Forrington | last post: by
3 posts views Thread by Brett | last post: by
7 posts views Thread by Thomas Pecha | last post: by
12 posts views Thread by Micah | last post: by
46 posts views Thread by Ian Boyd | last post: by
2 posts views Thread by Elioth | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.