javascript trace window

C

Chris

Can anyone point me to some code that will display messages, in a seperate
window, from javascipt. ie effectively a trace window?


TIA

Chris
 
C

Chris

Thanks v much, works a treat

one oddity though
in Opera 7.51 all OK
in IE 6 I had to alter the following

//startDebug.newWindow = window.open("",debugWindowName,
"scrollbars=yes,resizable=yes,width=700,height=500");
startDebug.newWindow = window.open("",null,
"scrollbars=yes,resizable=yes,width=700,height=500");

as it was throwing the following error

---------------------------
Error
---------------------------
A Runtime Error has occurred.
Do you wish to Debug?

Line: 15
Error: Invalid argument.
---------------------------
Yes No
---------------------------



Again my thanks

Chris


Robert said:
"Chris" <.@> said:
Can anyone point me to some code that will display messages, in a seperate
window, from javascipt. ie effectively a trace window?


This should help. Just run the file. The function debugOut displays a
message in the popup window created by startDebug. Be sure to enable
popup windows. My favorite line is:
debugOut("myVar = " + myVar);

alert is a point because you have to press enter and you loss was was
displayed.

Robert

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>javascript debug routines and tester</title>

<script type="text/javascript">

function startDebug(debugMode) {
startDebug.debugMode = debugMode;
if (startDebug.debugMode == true) {
var debugWindowName =
document.URL.substr(document.URL.lastIndexOf("/")+1);
var thePosition = debugWindowName.indexOf(".");
if (thePosition > 0)
{ debugWindowName = debugWindowName.substr(0,thePosition); }
startDebug.newWindow = window.open("",debugWindowName,
"scrollbars=yes,resizable=yes,width=700,height=500");
startDebug.newWindow.document.writeln(
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional' +
'//EN">' + "<html><header><title>Window " + debugWindowName +
"</title></header><body><p>The debug information " +
"that follows was generated on " + Date() + "</p>");
}
}


// Example invocation & print: DumpProperties(document,'document');

function dumpProperties(obj, obj_name)
{
var i;
// Example invocation & print: DumpProperties(document,'document');
if (startDebug.debugMode == true )
{
debugOut('In DumpProperties. obj=' + obj_name);
for (i in obj)
{

try
{
debugOut(obj_name + "." + i + " = " + obj);
}
catch(e)
{
debugOut("Error writing out structure. Was " + e + " for " + i);
}


}
}
}

function dumpPropertiesSorted(obj, obj_name)
{
var i;
var sorted = [ ];
var objCount = 0;

if (startDebug.debugMode == true )
{
debugOut('In DumpProperties. obj=' + obj_name);

for (i in obj)
{
sorted[objCount++] = i;
}

sorted.sort();

for ( i=0; i <sorted.length; i++)
{
try
{
debugOut(obj_name + "." + sorted + " = " +
obj[sorted]);
}
catch(e)
{
debugOut("Error writing out structure. Was " +
i + " for " + sorted);
}
}

} // if we are debugging.

}

function debugOut(obj)
{
var theString = "" + obj;
if (startDebug.debugMode == true ) {
// Ordering of the replacement string is important
var theString = theString.replace(
/&/g, "&amp;").replace(
/</g, "&lt;").replace(
/>/g, "&gt;");
// theString = theString.replace(/ /g, "&nbsp;");
startDebug.newWindow.document.writeln(theString + "<br>");
}
}



</SCRIPT>
</head>
<body>
<script>
var test = {a:1, c:3, b:2};
var testBoolean = true;

startDebug(true);

dumpProperties(test,"test");
debugOut("display the test object in sorted form");
dumpPropertiesSorted(test,"test");
debugOut(testBoolean);
debugOut("<b>&<\/b>");
</script>
<p>Debug routines and testing.</p>
</body>
</html>
 
R

Robert

Chris said:
Thanks v much, works a treat

Glad to have helped out.
one oddity though
;-)



in IE 6 I had to alter the following
Error: Invalid argument.
---------------------------

I suspect you had a special character in your file name. I am using
the filename as the debug window name. Web browsers seem to take the
input file name and use the escape function on them. For instance,
the space character in a file name is converted to %20. The IE
version of window.opend doesn't accept a % as part of the debug window
name.

I have posted a new version of the file below. I filter out the % and
all the other special characters.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>javascript debug routines and tester</title>

<script type="text/javascript">

// Popup the debug window.
// You need to have enabled popup windows

function startDebug(debugMode) {
startDebug.debugMode = debugMode;

if (startDebug.debugMode == true) {
var debugWindowName =
document.URL.substr(document.URL.lastIndexOf("/")+1);
var thePosition = debugWindowName.indexOf(".");
if (thePosition > 0)
{ debugWindowName = debugWindowName.substr(0,thePosition); }
// Get rid of special characters because IE won't accept them

debugWindowName =
debugWindowName.match(/[a-zA-Z0-9]/g).join("");

try
{ startDebug.newWindow = window.open("",debugWindowName,
"scrollbars=yes,resizable=yes,width=700,height=500"); }
catch(e)
{ debugWindowName = "debug";
startDebug.newWindow = window.open("",debugWindowName); }

startDebug.newWindow.document.writeln(
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional' +
'//EN">' + "<html><header><title>Window " + debugWindowName +
"</title></header><body><p>The debug information " +
"that follows was generated on " + Date() + "</p>");
}
}

// Example invocation & print: DumpProperties(document,'document');

function dumpProperties(obj, obj_name)
{
//Example invocation & print: DumpProperties(document,'document');
if (startDebug.debugMode == true )
{
debugOut('In DumpProperties. obj=' + obj_name);
for (var i in obj)
{
try
{ debugOut(obj_name + "." + i + " = " + obj); }
catch(e)
{ debugOut("Error writing out structure. Was " +
e + " for " + i); }
}
}
}

function dumpPropertiesSorted(obj, obj_name)
{
var i;
var sorted = [ ];
var objCount = 0;

if (startDebug.debugMode == true )
{
debugOut('In DumpProperties. obj=' + obj_name);

for (i in obj)
{
sorted[objCount++] = i;
}

sorted.sort();

for ( i=0; i <sorted.length; i++)
{
try
{ debugOut(obj_name + "." + sorted + " = " +
obj[sorted]); }
catch(e)
{ debugOut("Error writing out structure. Was " +
i + " for " + sorted); }
}

} // if we are debugging.
}

function debugOut(obj)
{
var theString = "" + obj;
if (startDebug.debugMode == true ) {
// Ordering of the replacement string is important
var theString = theString.replace(
/&/g, "&amp;").replace(
/</g, "&lt;").replace(
/>/g, "&gt;");
// theString = theString.replace(/ /g, "&nbsp;");
startDebug.newWindow.document.writeln(theString + "<br>");
}
}



</SCRIPT>
</head>
<body>
<script>
var test = {a:1, c:3, b:2};
var testBoolean = true;

startDebug(true);

dumpProperties(test,"test");
debugOut("display the test object in sorted form");
dumpPropertiesSorted(test,"test");
debugOut(testBoolean);
debugOut("<b>&<\/b>");

var myString = "abcDEF Ghi~123~ *&^ end";
debugOut("myString = " + myString +
" compressed: " +
escape(myString.match(/[a-zA-Z0-9]/g).join("") ) );

</script>
<p>Debug routines and testing.</p>
</body>
</html>
 
L

Lee

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

Opening and then writing into a window is unreliable.
It's much safer to have the new window load the content when it's ready.

function log(msg) {
globalHTML="<html><body><p>"+msg+"</p></body></html>";
window.open("javascript:eek:pener.globalHTML",
"logWindow",
"width=400,height=200,resizable").focus();
}

Since the window opens immediately, there's really no point in displaying the
date.
 

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
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top