根据这篇文章,它在测试版中,但不在发布中?
IE8 中的 console.log 发生了什么?
console.log 仅在您打开开发人员工具后可用(F12 切换打开和关闭)。有趣的是,在你打开它之后,你可以关闭它,然后仍然通过 console.log 调用发布到它,当你重新打开它时会看到这些。我认为这是一个错误,可能会被修复,但我们会看到。
我可能只会使用这样的东西:
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) }
}
更好的回退是这样的:
   var alertFallback = true;
   if (typeof console === "undefined" || typeof console.log === "undefined") {
     console = {};
     if (alertFallback) {
         console.log = function(msg) {
              alert(msg);
         };
     } else {
         console.log = function() {};
     }
   }
这是我对各种答案的看法。我想实际查看记录的消息,即使它们被触发时我没有打开 IE 控制台,所以我将它们推送到console.messages我创建的数组中。我还添加了一个功能console.dump(),方便查看整个日志。console.clear()将清空消息队列。
该解决方案还“处理”了其他 Console 方法(我相信它们都源自Firebug Console 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)
一些额外的信息
该行var args = Array.prototype.slice.call(arguments);从arguments对象创建一个数组。这是必需的,因为arguments 并不是真正的 Array。
trap()是任何 API 函数的默认处理程序。我将参数传递给 ,message以便您获得传递给任何 API 调用(不仅仅是console.log)的参数的日志。
编辑
我添加了一个额外的数组console.raw,它完全按照传递给trap(). 我意识到args.join(' ')将对象转换为字符串"[object Object]",这有时可能是不可取的。感谢bfontaine的建议。
值得注意的是,console.log在 IE8 中并不是真正的 Javascript 函数。它不支持applyorcall方法。
假设您不关心警报的回退,这里有一种更简洁的方法来解决 Internet Explorer 的缺点:
var console=console||{"log":function(){}};