debug message to Javascript console

Discussion in 'Javascript' started by KC, Dec 3, 2006.

  1. KC

    KC Guest

    Hi,

    I don't know why this question is so hard to have an answer ? I search
    Google for
    a while ... and can't find any "good" answer. Using "throw new
    Error(msg)" does
    work ... but I believe most people asking this question is try to find
    method similar to
    'printf()' instead of exception which may stop the script.

    So please allow me to repeat this question again:
    how to print a "Hello World" message to Javascript console (from
    Javascript
    based web page writer's point of view).

    The "Hello World" should be a 'normal message' instead of 'error
    message'.

    So far, I found 'dump()' may be the solution if you can turn on some
    browser's option ...
    but don't know which one I should use for Firefox ??

    It will be great if someone could provide a complete example instead of
    just part of the
    code.

    Or, does Javascript Console ... just not designed for this kind of
    purpose ???


    Thanks
    KC
    KC, Dec 3, 2006
    #1
    1. Advertising

  2. KC wrote:

    > So please allow me to repeat this question again:
    > how to print a "Hello World" message to Javascript console (from
    > Javascript
    > based web page writer's point of view).
    >
    > The "Hello World" should be a 'normal message' instead of 'error
    > message'.


    It depends on the browser having a console and exposing a method to
    write to it, Opera allows you to use window.opera.postError

    window.opera.postError('typeof GOD: ' + (typeof GOD))

    With Mozilla I think if you install the Firebug extension then it gives
    you a way to write messages to the error console.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Dec 3, 2006
    #2
    1. Advertising

  3. KC

    KC Guest

    Thanks Martin,

    Firebug ... that's even better than Javascript Console :)
    and it's easy to achieve the simple 'Hello World' purpose by:

    if (console) { // check if Firebug installed
    console.log('Hello World');
    }

    Regards
    KC



    Martin Honnen wrote:
    > KC wrote:
    >
    > > So please allow me to repeat this question again:
    > > how to print a "Hello World" message to Javascript console (from
    > > Javascript
    > > based web page writer's point of view).
    > >
    > > The "Hello World" should be a 'normal message' instead of 'error
    > > message'.

    >
    > It depends on the browser having a console and exposing a method to
    > write to it, Opera allows you to use window.opera.postError
    >
    > window.opera.postError('typeof GOD: ' + (typeof GOD))
    >
    > With Mozilla I think if you install the Firebug extension then it gives
    > you a way to write messages to the error console.
    >
    >
    > --
    >
    > Martin Honnen
    > http://JavaScript.FAQTs.com/
    KC, Dec 3, 2006
    #3
  4. KC wrote:

    > Firebug ... that's even better than Javascript Console :)
    > and it's easy to achieve the simple 'Hello World' purpose by:
    >
    > if (console) { // check if Firebug installed
    > console.log('Hello World');
    > }


    That only seems to work. Because, as a basic feature of the language, if an
    identifier is not defined, you cannot test whether it was defined or not
    this way. Meaning that if Firebug is not installed, this results in a
    ReferenceError run-time error because `console' is not a property of the
    Global Object.

    Instead, you are looking for

    if (typeof console != "undefined" // test for Firebug
    && typeof console.log == "function") // test for method
    {
    console.log('Hello World');
    }

    Usually one wants to define a wrapper method that can be called for that
    purpose, such as

    function dmsg(sText, sMethod)
    {
    if (!sMethod) sMethod = "log";

    if (typeof console != "undefined"
    && typeof console[sMethod] == "function")
    {
    console[sMethod](sText);
    }
    }


    PointedEars
    Thomas 'PointedEars' Lahn, Dec 9, 2006
    #4
  5. KC

    Matt Kruse Guest

    Thomas 'PointedEars' Lahn wrote:
    > Usually one wants to define a wrapper method that can be called for
    > that purpose, such as
    > function dmsg(sText, sMethod) {
    > if (!sMethod) sMethod = "log";
    > if (typeof console != "undefined"
    > && typeof console[sMethod] == "function") {
    > console[sMethod](sText);
    > }
    > }


    The existence of 'console' is not likely to change once the page is loaded,
    so there is no need to check for its existence every time. If you get rid of
    the 'method' abstraction, you can do this:

    var dmsg = (function(){
    if (typeof(console)!="undefined"
    && typeof(console.log)=="function") {
    return function(msg) { console.log(msg); };
    }
    return function (msg) { }
    })();

    Just another option...

    --
    Matt Kruse
    http://www.JavascriptToolbox.com
    http://www.AjaxToolbox.com
    Matt Kruse, Dec 10, 2006
    #5
    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. Ben Finney
    Replies:
    2
    Views:
    976
    Egor Bolonev
    Jun 30, 2003
  2. Egor Bolonev
    Replies:
    0
    Views:
    464
    Egor Bolonev
    Jun 30, 2003
  3. Serge Terekhoff
    Replies:
    0
    Views:
    631
    Serge Terekhoff
    Jun 30, 2003
  4. Miktor
    Replies:
    7
    Views:
    11,996
    Joe Bacigalupa
    Aug 9, 2005
  5. ion
    Replies:
    10
    Views:
    147
Loading...

Share This Page