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>Exampl e Debug Console</TITLE>
<SCRIPT LANGUAGE="JavaS cript">
// Global variable for debug window. Function startDebug must be
// called before first call to the Out function.
var debugMode = "yes";
var newWindow = "";
var debugWindowName = "debugconso le"
// 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(
"",debugWindowN ame,"scrollbars =yes,resizable= yes,width=700,h eight=500");
newWindow.docum ent.writeln(
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">');
newWindow.docum ent.writeln("<h tml>");
newWindow.docum ent.writeln(
"<header><title >Window " + debugWindowName + "</title></header>");
newWindow.docum ent.writeln(
"<body><p>T he debug information that follows was generated on "
+ aDate + "</p>");
}
}
// Debug functions
function DumpProperties( obj, obj_name)
{
// Example invocation: DumpProperties( document,'docum ent');
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.leng th;i++)
{
if (obj.substr(i,1 ) == "<")
newString += "<";
else if (obj.substr(i,1 ) == ">")
newString += ">";
else if (obj.substr(i,1 ) == "&")
newString += "&";
else
newString += obj.substr(i,1) ;
}
newWindow.docum ent.writeln(new String + "<br>");
}
return;
}
</script>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaS cript">
//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,'locat ion');
Out("Let's see if we can dump out the whole document object");
DumpProperties( document,'docum ent');
</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>
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>Exampl e Debug Console</TITLE>
<SCRIPT LANGUAGE="JavaS cript">
// Global variable for debug window. Function startDebug must be
// called before first call to the Out function.
var debugMode = "yes";
var newWindow = "";
var debugWindowName = "debugconso le"
// 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(
"",debugWindowN ame,"scrollbars =yes,resizable= yes,width=700,h eight=500");
newWindow.docum ent.writeln(
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">');
newWindow.docum ent.writeln("<h tml>");
newWindow.docum ent.writeln(
"<header><title >Window " + debugWindowName + "</title></header>");
newWindow.docum ent.writeln(
"<body><p>T he debug information that follows was generated on "
+ aDate + "</p>");
}
}
// Debug functions
function DumpProperties( obj, obj_name)
{
// Example invocation: DumpProperties( document,'docum ent');
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.leng th;i++)
{
if (obj.substr(i,1 ) == "<")
newString += "<";
else if (obj.substr(i,1 ) == ">")
newString += ">";
else if (obj.substr(i,1 ) == "&")
newString += "&";
else
newString += obj.substr(i,1) ;
}
newWindow.docum ent.writeln(new String + "<br>");
}
return;
}
</script>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="JavaS cript">
//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,'locat ion');
Out("Let's see if we can dump out the whole document object");
DumpProperties( document,'docum ent');
</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>
Comment