How can I print a message to the error console, preferably including a variable?
For example, something like:
print(\'x=%d\', x);
With es6 syntax you can use:
console.log(`x = ${x}`);
console.log("your message here");
working for me.. i'm searching for this.. i used Firefox. here is my Script.
$('document').ready(function() {
console.log('all images are loaded');
});
works in Firefox and Chrome.
A note about 'throw()' mentioned above. It seems that it stops execution of the page completely (I checked in IE8) , so it's not very useful for logging "on going processes" (like to track a certain variable...)
My suggestion is perhaps to add a textarea element somewhere in your document and to change (or append to) its value (which would change its text) for logging information whenever needed...
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');
Install Firebug and then you can use console.log(...)
and console.debug(...)
, etc. (see the documentation for more).
One good way to do this that works cross-browser is outlined in Debugging JavaScript: Throw Away Your Alerts!.