我使用Firebug,并有一些语句像:

console.log("...");

在我的页面上。在IE8(可能是更早的版本),我得到脚本错误说“控制台”是未定义的。我试着把这个放在我页面的顶部:

<script type="text/javascript">
    if (!console) console = {log: function() {}};
</script>

我还是会得到错误。有办法消除错误吗?


当前回答

对于IE8或控制台支持仅限于console.log(没有调试,跟踪,…),您可以执行以下操作:

如果console或console.log未定义:为 控制台函数(跟踪,调试,日志,…) 窗口。控制台= { Debug: function(){},…}; 否则,如果console.log被定义(IE8)和console.debug(任何其他)没有定义:重定向所有日志功能到console.log,这允许保留这些日志! 窗口。控制台= { 调试:window.console.log,…};

不确定在各种IE版本中的assert支持,但欢迎任何建议。也在这里发布了这个答案:我如何在Internet Explorer中使用控制台登录?

其他回答

在使用控制台之前,将以下内容粘贴到JavaScript的顶部:

/**
 * Protect window.console method calls, e.g. console is not defined on IE
 * unless dev tools are open, and IE doesn't define console.debug
 * 
 * Chrome 41.0.2272.118: debug,error,info,log,warn,dir,dirxml,table,trace,assert,count,markTimeline,profile,profileEnd,time,timeEnd,timeStamp,timeline,timelineEnd,group,groupCollapsed,groupEnd,clear
 * Firefox 37.0.1: log,info,warn,error,exception,debug,table,trace,dir,group,groupCollapsed,groupEnd,time,timeEnd,profile,profileEnd,assert,count
 * Internet Explorer 11: select,log,info,warn,error,debug,assert,time,timeEnd,timeStamp,group,groupCollapsed,groupEnd,trace,clear,dir,dirxml,count,countReset,cd
 * Safari 6.2.4: debug,error,log,info,warn,clear,dir,dirxml,table,trace,assert,count,profile,profileEnd,time,timeEnd,timeStamp,group,groupCollapsed,groupEnd
 * Opera 28.0.1750.48: debug,error,info,log,warn,dir,dirxml,table,trace,assert,count,markTimeline,profile,profileEnd,time,timeEnd,timeStamp,timeline,timelineEnd,group,groupCollapsed,groupEnd,clear
 */
(function() {
  // Union of Chrome, Firefox, IE, Opera, and Safari console methods
  var methods = ["assert", "cd", "clear", "count", "countReset",
    "debug", "dir", "dirxml", "error", "exception", "group", "groupCollapsed",
    "groupEnd", "info", "log", "markTimeline", "profile", "profileEnd",
    "select", "table", "time", "timeEnd", "timeStamp", "timeline",
    "timelineEnd", "trace", "warn"];
  var length = methods.length;
  var console = (window.console = window.console || {});
  var method;
  var noop = function() {};
  while (length--) {
    method = methods[length];
    // define undefined methods as noops to prevent errors
    if (!console[method])
      console[method] = noop;
  }
})();

函数闭包包装器的作用域是不定义任何变量。这可以防止未定义的控制台和未定义的console.debug(以及其他缺失的方法)。

编辑:我注意到HTML5 Boilerplate在其js/plugins.js文件中使用了类似的代码,如果你正在寻找一个将(可能)保持最新的解决方案。

在TypeScript中控制台的存根:

if (!window.console) {
console = {
    assert: () => undefined,
    clear: () => undefined,
    count: () => undefined,
    debug: () => undefined,
    dir: () => undefined,
    dirxml: () => undefined,
    error: () => undefined,
    group: () => undefined,
    groupCollapsed: () => undefined,
    groupEnd: () => undefined,
    info: () => undefined,
    log: () => undefined,
    msIsIndependentlyComposed: (e: Element) => false,
    profile: () => undefined,
    profileEnd: () => undefined,
    select: () => undefined,
    time: () => undefined,
    timeEnd: () => undefined,
    trace: () => undefined,
    warn: () => undefined,
    }
};

您可以在Firefox中直接使用console.log(…),但不能在ie中使用。在ie中,你必须使用window.console。

Try

if (!window.console) console = ...

未定义的变量不能直接引用。但是,所有全局变量都是全局上下文(浏览器为窗口)同名的属性,访问未定义的属性是可以的。

或者使用if (typeof console === 'undefined') console =…如果你想避开神奇的变量窗口,请参阅@Tim Down的回答。

在IE9的子窗口中运行console.log时遇到类似问题,由window创建。打开功能。

在这种情况下,控制台似乎只在父窗口中定义,在刷新子窗口之前没有定义。同样适用于子窗口的子窗口。

我处理这个问题通过包装log在下一个函数(下面是模块的片段)

getConsole: function()
    {
        if (typeof console !== 'undefined') return console;

        var searchDepthMax = 5,
            searchDepth = 0,
            context = window.opener;

        while (!!context && searchDepth < searchDepthMax)
        {
            if (typeof context.console !== 'undefined') return context.console;

            context = context.opener;
            searchDepth++;
        }

        return null;
    },
    log: function(message){
        var _console = this.getConsole();
        if (!!_console) _console.log(message);
    }