1

Is there a cross browser way of implementing console.log functionality, the official supported browser at one of my client is still IE7/IE8.

As development is tested with Firefox for its debugging capabilities, often come across issues on release as some obscure flow still left with un commented console.log statements which doesn't fly with IE

looking for kind of, if anybody does use an elegant way, would love to learn.

function log(msg){
   if(IE) alert(msg)
   else
     console.log(msg)
}
4

2 回答 2

2

这是一个选项:

if (typeof console === "undefined" || typeof console.log === "undefined") {  
    console = {};
    console.log = function(msg) {
        alert(msg);
    };
}

编辑:正如其他人所指出的,您确实应该确保 console.log 不会出现在生产代码中。

于 2013-08-28T20:30:33.980 回答
2

看看类似的东西:

log4javascript

或者

log4js

他们可能是您正在寻找的东西(还有其他人)

然后,您可以检测控制台对象在您正在使用的浏览器上是否可用,如果没有,则从这些库中分配适当的功能。

尽管阅读了您的问题,但它似乎确实是一个 XY 问题。

于 2013-08-28T20:51:38.477 回答