views:

18300

answers:

4

Does anyone know how to print debug messages in the Google Chrome Javascript Console?

Please note that the Javascript Console is not the same as the Javascript Debugger, they have different syntaxes AFAIK, so the print command in Javascript Debugger will not work here. In the Javascript Console, print() will send the parameter to the printer.

There is a related question already on SO, but it does not solve my problem: http://stackoverflow.com/questions/45965/how-do-i-use-the-javascript-console-in-google-chrome

+30  A: 

Executing following code from the browser address bar:

javascript: console.log(2);

successfully prints message to the "JavaScript Console" in Google Chrome.

Sergey Ilinsky
Thanks, that's exactly what I needed.
DrJokepu
+2  A: 

Just a quick warning - if you want to test in IE without removing all console.log()'s, you'll need to use FireBug Lite (http://getfirebug.com/lite.html) or you'll get some not particularly friendly errors.

(or create your own console.log() which just returns false)

Andru
+16  A: 

Improving on Andru's idea, you can write a script which creates console functions if they don't exist:

if (!window.console) console = {};
console.log = console.log || function(){};
console.warn = console.warn || function(){};
console.error = console.error || function(){};
console.info = console.info || function(){};

Then, use any of the following:

console.log(...);
console.error(...);
console.info(...);
console.warn(...);

These functions will log different types of items (which can be filtered based on log, info, error or warn) and will not cause errors when console is not available. These functions will work in Firebug and Chrome consoles.

Delan Azabani
this is great! thanks a lot :D
Agos
A: 
Vegar