473,387 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

debug message to Javascript console

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
4 3074
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Robert | last post by:
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,...
9
by: Nick Forrington | last post by:
Hi, I'm making a program and have a static Console class that I'm using to output things, these get sent to the console and also to the graphics on screen (I'm using SDL). One thing I'm having a...
10
by: ion | last post by:
Hi! Is there a way to write messages to the javascript console? Like a debug.write() method? Thanks! Ion
4
by: Bill Cohagan | last post by:
I'm writing a console app (in C#) and I want to be able to redirect the standard input/output streams when it's run at a command prompt. IOW I want to support the "<" and ">" redirection syntax....
3
by: mikeorb | last post by:
I'm debugging some JavaScript. In Firefox I can use dump(msg) to print a message on their JS console (see http://kb.mozillazine.org/Viewing_dump()_output). Likewise, Opera has the...
7
by: Thomas Pecha | last post by:
Sorry for all who think this is easy, I was not able to handle this Coming from VB6 where with simple debug.print strAString you could write to debug window, I am totalling failing in vb.net...
46
by: Ian Boyd | last post by:
IIS5, on a Windows 2000 Server machine. Debeg.WriteLine "Hello, world!" How can i view it?
3
by: TC | last post by:
I'm trying to debug a console application, but I can't see the console output. I've seen many references which say that console output is supposed to appear on the Output window when the...
2
by: joelkeepup | last post by:
Hi, I made a change this morning and now im getting an error that says either "a is undefined or null" or "e is undefined or null" the microsoft ajax line is below, I have no idea how to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.