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


当前回答

我使用沃尔特的方法从上面(见:https://stackoverflow.com/a/14246240/3076102)

我在这里找到了一个解决方案https://stackoverflow.com/a/7967670来正确地显示对象。

这意味着trap函数变成:

function trap(){
    if(debugging){
        // 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 index;
        for (index = 0; index < args.length; ++index) {
            //fix for objects
            if(typeof args[index] === 'object'){ 
                args[index] = JSON.stringify(args[index],null,'\t').replace(/\n/g,'<br>').replace(/\t/g,'&nbsp;&nbsp;&nbsp;');
            }
        }
        var message = args.join(' ');
        console.messages.push(message);
        // instead of a fallback function we use the next few lines to output logs
        // at the bottom of the page with jQuery
        if($){
            if($('#_console_log').length == 0) $('body').append($('<div />').attr('id', '_console_log'));
            $('#_console_log').append(message).append($('<br />'));
        }
    }
} 

我希望这对你有帮助:-)

其他回答

值得注意的是,IE8中的console.log并不是一个真正的Javascript函数。它不支持apply或call方法。

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

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

我在github上找到了这个:

// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function f() {
    log.history = log.history || [];
    log.history.push(arguments);
    if (this.console) {
        var args = arguments,
            newarr;
        args.callee = args.callee.caller;
        newarr = [].slice.call(args);
        if (typeof console.log === 'object') log.apply.call(console.log, console, newarr);
        else console.log.apply(console, newarr);
    }
};

// make it safe to use console.log always
(function(a) {
    function b() {}
    for (var c = "assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","), d; !! (d = c.pop());) {
        a[d] = a[d] || b;
    }
})(function() {
    try {
        console.log();
        return window.console;
    } catch(a) {
        return (window.console = {});
    }
} ());

我喜欢这个方法(使用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,请不要崩溃”

typeof console=="undefined"&&(console={});typeof console.log=="undefined"&&(console.log=function(){});