无错误控制台日志记录

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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值