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


当前回答

以下是我对各种答案的看法。我想真正看到记录的消息,即使它们被触发时我没有打开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]",这有时可能是不可取的。谢谢你的建议。

其他回答

它适用于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) }
}

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

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

我在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 = {});
    }
} ());

在html ....中制作自己的控制台: -) 这是可以改进的,但你可以从以下开始:

if (typeof console == "undefined" || typeof console.log === "undefined") {
    var oDiv=document.createElement("div");
    var attr = document.createAttribute('id'); attr.value = 'html-console';
    oDiv.setAttributeNode(attr);


    var style= document.createAttribute('style');
    style.value = "overflow: auto; color: red; position: fixed; bottom:0; background-color: black; height: 200px; width: 100%; filter: alpha(opacity=80);";
    oDiv.setAttributeNode(style);

    var t = document.createElement("h3");
    var tcontent = document.createTextNode('console');
    t.appendChild(tcontent);
    oDiv.appendChild(t);

    document.body.appendChild(oDiv);
    var htmlConsole = document.getElementById('html-console');
    window.console = {
        log: function(message) {
            var p = document.createElement("p");
            var content = document.createTextNode(message.toString());
            p.appendChild(content);
            htmlConsole.appendChild(p);
        }
    };
}