Debug - Your own console

Discussion in 'Javascript' started by Robert, Feb 8, 2004.

  1. Robert

    Robert Guest

    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>
     
    Robert, Feb 8, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Siegl
    Replies:
    1
    Views:
    784
  2. Saverio M.
    Replies:
    0
    Views:
    535
    Saverio M.
    Jul 3, 2006
  3. Ben Finney
    Replies:
    2
    Views:
    1,001
    Egor Bolonev
    Jun 30, 2003
  4. c676228
    Replies:
    3
    Views:
    185
    c676228
    Sep 14, 2006
  5. David Filmer
    Replies:
    17
    Views:
    276
    J. Romano
    Aug 18, 2004
Loading...

Share This Page