473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 2060
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Nick Forrington | last post by:
Hi, I'm making a program and have a static Console class that I'm using to output things, these get sent to the console and also to the graphics on screen (I'm using SDL). One thing I'm having a...
4
by: Bill Cohagan | last post by:
I'm writing a console app (in C#) and I want to be able to redirect the standard input/output streams when it's run at a command prompt. IOW I want to support the "<" and ">" redirection syntax....
3
by: Brett | last post by:
I use the following to output to the console during debugging: catch(Exception ex) { Debug.WriteLine("Error: " + ex.Message); } Many of the times on the console, the above is appended on the...
7
by: Thomas Pecha | last post by:
Sorry for all who think this is easy, I was not able to handle this Coming from VB6 where with simple debug.print strAString you could write to debug window, I am totalling failing in vb.net...
12
by: Micah | last post by:
I am looking for a way to tell if the program is compiled in debug mode, I want the application I am writing to spit out a message to QA if a error occured and if it is in debug mode, but if it is...
46
by: Ian Boyd | last post by:
IIS5, on a Windows 2000 Server machine. Debeg.WriteLine "Hello, world!" How can i view it?
2
by: Elioth | last post by:
Hi, How I Debug a Windows Services in VB 2005? Thanks for all help. Elioth
3
by: TC | last post by:
I'm trying to debug a console application, but I can't see the console output. I've seen many references which say that console output is supposed to appear on the Output window when the...
2
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.