How can I print a message to the error console, preferably including a variable? e.g., something like
print('x=%d', x);
How can I print a message to the error console, preferably including a variable? e.g., something like
print('x=%d', x);
Install Firebug and then you can use console.log(...)
and console.debug(...)
, etc (see the docs for more).
If you are using Firebug and need to support IE, Safari or Opera as well, Firebug Lite adds console.log() support to these browsers.
The WebKit Web Inspector also supports FireBug's console API (just a minor addition to Dan's answer above)
One good way to do this that works cross-browser is outlined in http://www.sitepoint.com/blogs/2008/08/22/debugging-javascript-throw-away-your-alerts/
Exceptions are logged into the javascript console. You can use that if you want to keep Firebug disabled.
function log(msg) {
setTimeout(function() {
throw new Error(msg);
}, 0);
}
Usage:
log('Hello World');
log('another message');