By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,297 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

debug message to Javascript console

P: n/a
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

Dec 3 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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/
Dec 3 '06 #2

P: n/a
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

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/
Dec 3 '06 #3

P: n/a
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
Dec 9 '06 #4

P: n/a
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
Dec 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.