debug message to Javascript console

K

KC

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
 
M

Martin Honnen

KC said:
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.
 
K

KC

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
 
T

Thomas 'PointedEars' Lahn

KC said:
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
 
M

Matt Kruse

Thomas said:
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...
 

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
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top