根据这篇文章,它在测试版中,但它不在发行版中?


当前回答

假设你不关心提醒的备用方法,这里有一个更简洁的方法来解决ie的缺点:

var console=console||{"log":function(){}};

其他回答

我喜欢这个方法(使用jquery的doc ready)…它可以让你使用控制台甚至在ie…唯一的问题是,如果你在页面加载后打开ie的开发工具,你需要重新加载页面……

如果把所有的函数都考虑进去,可能会更圆滑一些,但我只使用log,这就是我要做的。

//one last double check against stray console.logs
$(document).ready(function (){
    try {
        console.log('testing for console in itcutils');
    } catch (e) {
        window.console = new (function (){ this.log = function (val) {
            //do nothing
        }})();
    }
});

以下是我对各种答案的看法。我想真正看到记录的消息,即使它们被触发时我没有打开IE控制台,所以我把它们推到一个控制台。我创建的消息数组。我还添加了一个console.dump()函数,以方便查看整个日志。Console.clear()将清空消息队列。

这个解决方案还“处理”了其他控制台方法(我相信它们都起源于Firebug控制台API)

最后,该解决方案采用IIFE形式,因此不会污染全局范围。回退函数参数在代码的底部定义。

我只是把它放在我的主JS文件中,包括在每一页,然后忘记它。

(function (fallback) {    

    fallback = fallback || function () { };

    // function to trap most of the console functions from the FireBug Console API. 
    var trap = function () {
        // create an Array from the arguments Object           
        var args = Array.prototype.slice.call(arguments);
        // console.raw captures the raw args, without converting toString
        console.raw.push(args);
        var message = args.join(' ');
        console.messages.push(message);
        fallback(message);
    };

    // redefine console
    if (typeof console === 'undefined') {
        console = {
            messages: [],
            raw: [],
            dump: function() { return console.messages.join('\n'); },
            log: trap,
            debug: trap,
            info: trap,
            warn: trap,
            error: trap,
            assert: trap,
            clear: function() { 
                  console.messages.length = 0; 
                  console.raw.length = 0 ;
            },
            dir: trap,
            dirxml: trap,
            trace: trap,
            group: trap,
            groupCollapsed: trap,
            groupEnd: trap,
            time: trap,
            timeEnd: trap,
            timeStamp: trap,
            profile: trap,
            profileEnd: trap,
            count: trap,
            exception: trap,
            table: trap
        };
    }

})(null); // to define a fallback function, replace null with the name of the function (ex: alert)

一些额外的信息

line var args = Array.prototype.slice.call(参数);从参数Object创建一个数组。这是必需的,因为arguments并不是真正的数组。

trap()是任何API函数的默认处理程序。我将参数传递给message,以便您获得传递给任何API调用的参数的日志(不仅仅是console.log)。

Edit

我增加了一个额外的数组控制台。捕获传递给trap()的参数。我意识到args。join(' ')正在将对象转换为字符串"[object object]",这有时可能是不可取的。谢谢你的建议。

假设你不关心提醒的备用方法,这里有一个更简洁的方法来解决ie的缺点:

var console=console||{"log":function(){}};

它适用于IE8。点击F12打开IE8的开发者工具。

>>console.log('test')
LOG: test

console.log只有在打开开发人员工具(F12来切换它的打开和关闭)后才可用。 有趣的是,在你打开它之后,你可以关闭它,然后仍然通过console.log调用发布到它,这些将在你重新打开它时看到。 我认为这是一个bug,可能会被修复,但我们将拭目以待。

我可能会用这样的东西:

function trace(s) {
  if ('console' in self && 'log' in console) console.log(s)
  // the line below you might want to comment out, so it dies silent
  // but nice for seeing when the console is available or not.
  else alert(s)
}

更简单的是:

function trace(s) {
  try { console.log(s) } catch (e) { alert(s) }
}