我如何打印消息到错误控制台,最好包括一个变量?
例如,像这样:
print('x=%d', x);
我如何打印消息到错误控制台,最好包括一个变量?
例如,像这样:
print('x=%d', x);
当前回答
如果你使用Safari,你可以写
console.log("your message here");
它就出现在浏览器的控制台上。
其他回答
安装Firebug,然后你就可以使用console.log(…)和console.debug(…)等等(更多信息请参阅文档)。
console.error(message); // Outputs an error message to the Web Console
console.log(message); // Outputs a message to the Web Console
console.warn(message); // Outputs a warning message to the Web Console
console.info(message); // Outputs an informational message to the Web Console. In some browsers it shows a small "i" in front of the message.
你也可以添加CSS:
console.log('%c My message here', "background: blue; color: white; padding-left:10px;");
更多信息可以在这里找到:https://developer.mozilla.org/en-US/docs/Web/API/console
像往常一样,Internet Explorer是让您无法使用console.log()的笨重的旱冰鞋。
jQuery的日志可以很容易地进行调整,但是在任何地方都要添加它是一种痛苦。如果你正在使用jQuery,一个解决方案是把它放在你的jQuery文件的最后,首先缩小:
function log()
{
if (arguments.length > 0)
{
// Join for graceful degregation
var args = (arguments.length > 1) ? Array.prototype.join.call(arguments, " ") : arguments[0];
// This is the standard; Firebug and newer WebKit browsers support this.
try {
console.log(args);
return true;
} catch(e) {
// Newer Opera browsers support posting erros to their consoles.
try {
opera.postError(args);
return true;
}
catch(e)
{
}
}
// Catch all; a good old alert box.
alert(args);
return false;
}
}
异常被记录到JavaScript控制台。如果你想禁用Firebug,你可以使用它。
function log(msg) {
setTimeout(function() {
throw new Error(msg);
}, 0);
}
用法:
log('Hello World');
log('another message');
如果您正在使用Firebug,并且需要支持IE、Safari或Opera, Firebug Lite为这些浏览器添加了console.log()支持。