var Fb = {}; //An empty object literal for holding the function
Fb.log = function(obj, consoleMethod) {
if (window.console && window.console.firebug && window.console.firebug.replace(/^\s\s*/, '').replace(/\s\s*$/, '') !== '') {
if (typeof consoleMethod === "string" && typeof console[consoleMethod] === "function") {
console[consoleMethod](obj);
} else {
console.log(obj);
}
}
}
如果您在JavaScript代码中保留console.log,console.info等消息,并在浏览器(如IE)中打开页面,则可能会完全停止加载页面,提示“未定义控制台”,尤其是在您的用户使用非IE8浏览器的情况下。
如果需要,此代码段将使您可以在代码中保留日志消息,并且页面将在IE或不支持控制台消息的任何其他浏览器中正确呈现。
用法
Fb.log("This will be logged");
Fb.log("This will be displayed in console as info", "info");
FB.log函数接受两个参数,第一个是要在firebug控制台中显示的“项目”,第二个是要用于日志记录的firebug方法,例如info,error等。您忽略第二个参数,结果将等同于console.log()
简单的仅日志方式
function ltc(what) {
try {
console.log(what);
}
catch (e) {}
finally {
return;
}
}
ltc("message");
翻译自: https://css-tricks.com/snippets/javascript/error-free-firebug-logging/