Javascript ';控制台';是Internet Explorer的未定义错误

Javascript ';控制台';是Internet Explorer的未定义错误,javascript,internet-explorer,internet-explorer-8,ie-developer-tools,Javascript,Internet Explorer,Internet Explorer 8,Ie Developer Tools,我使用的是Firebug,有如下声明: console.log("..."); 在我的页面中。在IE8(可能也是更早的版本)中,我收到脚本错误,说“控制台”未定义。我试着把这个放在我的页面顶部: <script type="text/javascript"> if (!console) console = {log: function() {}}; </script> 如果(!console)console={log:function(){}; 但我还是会

我使用的是Firebug,有如下声明:

console.log("...");
在我的页面中。在IE8(可能也是更早的版本)中,我收到脚本错误,说“控制台”未定义。我试着把这个放在我的页面顶部:

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

如果(!console)console={log:function(){};
但我还是会犯错误。有没有办法消除这些错误?

试试看

if (!window.console) console = ...
无法直接引用未定义的变量。但是,所有全局变量都是全局上下文的同名属性(
window
,对于浏览器),访问未定义的属性是可以的

或者使用
if(typeof console=='undefined')console=…
如果您想避免使用神奇变量
窗口,请参阅。

尝试

if (!window.console) console = ...
无法直接引用未定义的变量。但是,所有全局变量都是全局上下文的同名属性(
window
,对于浏览器),访问未定义的属性是可以的


或者使用
if(typeof console=='undefined')console=…
如果您想避免使用神奇变量
窗口,请参阅。

在我的脚本中,我可以使用速记:

window.console && console.log(...) // only log if the function exists
或者,如果编辑每个console.log行是不可能或不可行的,我会创建一个假控制台:

// check to see if console exists. If not, create an empty object for it,
// then create and empty logging function which does nothing. 
//
// REMEMBER: put this before any other console.log calls
!window.console && (window.console = {} && window.console.log = function () {});
/**
 * 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;
  }
})();

在我的脚本中,我要么使用速记:

window.console && console.log(...) // only log if the function exists
或者,如果编辑每个console.log行是不可能或不可行的,我会创建一个假控制台:

// check to see if console exists. If not, create an empty object for it,
// then create and empty logging function which does nothing. 
//
// REMEMBER: put this before any other console.log calls
!window.console && (window.console = {} && window.console.log = function () {});
/**
 * 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;
  }
})();

另一种选择是
类型的
运算符:

if (typeof console == "undefined") {
    this.console = {log: function() {}};
}

另一种选择是使用日志库,如my own。

另一种选择是
类型的
操作符:

if (typeof console == "undefined") {
    this.console = {log: function() {}};
}

另一种选择是使用日志库,比如我自己的。

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

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

如果你在IE8中打开了
开发工具,你可以使用
console.log()
,也可以使用脚本选项卡上的
console
文本框。

你可以使用
console.log()
如果您在IE8中打开了
开发工具
,也可以使用脚本选项卡上的
控制台
文本框。

我正在使用;我对css做了一些修改,使它看起来更漂亮,但效果很好;我对css做了一些修改,使它看起来更漂亮,但效果很好。

在IE9中,如果控制台未打开,则此代码:

alert(typeof console);
将显示“object”,但此代码

alert(typeof console.log);
将抛出TypeError异常,但不返回未定义的值

因此,代码的保证版本将类似于:

try {
    if (window.console && window.console.log) {
        my_console_log = window.console.log;
    }
} catch (e) {
    my_console_log = function() {};
}

在IE9中,如果控制台未打开,则此代码:

alert(typeof console);
将显示“object”,但此代码

alert(typeof console.log);
将抛出TypeError异常,但不返回未定义的值

因此,代码的保证版本将类似于:

try {
    if (window.console && window.console.log) {
        my_console_log = window.console.log;
    }
} catch (e) {
    my_console_log = function() {};
}

在JavaScript顶部粘贴以下内容(在使用控制台之前):

函数闭包包装器用于限定变量的范围,从而不定义任何变量。这可以防止未定义的
console
和未定义的
console.debug
(以及其他缺少的方法)


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

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

函数闭包包装器用于限定变量的范围,从而不定义任何变量。这可以防止未定义的
console
和未定义的
console.debug
(以及其他缺少的方法)


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

您可以使用下面的代码来提供额外的保险,即您已经覆盖了所有基础。首先使用
typeof
可以避免任何
未定义的
错误。使用
=
还将确保类型的名称实际上是字符串“undefined”。最后,您需要向函数签名添加一个参数(我任意选择了
logMsg
),以确保一致性,因为您确实会将希望打印到控制台的内容传递给log函数。这还可以保持intellisense的准确性,避免JS感知IDE中出现任何警告/错误

if(!window.console || typeof console === "undefined") {
  var console = { log: function (logMsg) { } };
}

您可以使用下面的内容来提供一个额外的保险级别,您已经覆盖了所有基础。首先使用
typeof
可以避免任何
未定义的
错误。使用
=
还将确保类型的名称实际上是字符串“undefined”。最后,您需要向函数签名添加一个参数(我任意选择了
logMsg
),以确保一致性,因为您确实会将希望打印到控制台的内容传递给log函数。这还可以保持intellisense的准确性,避免JS感知IDE中出现任何警告/错误

if(!window.console || typeof console === "undefined") {
  var console = { log: function (logMsg) { } };
}

要在IE中进行调试,请查看此

要在IE中进行调试,请查看此

以获得IE8或仅限于console.log的console支持(无调试、跟踪等)。您可以执行以下操作:

  • 如果未定义console或console.log:为 控制台功能(跟踪、调试、日志等)

    window.console={
    调试:函数(){},…}

  • 否则,如果定义了console.log(IE8)而未定义console.debug(任何其他):将所有日志函数重定向到console.log,则允许保留这些日志

    window.console={
    调试:window.console.log,…}


不确定各种IE版本中的断言支持,但欢迎提供任何建议。也在此处发布了此答案:

对于IE8或仅限于console.log的console支持(无调试、跟踪等),您可以执行以下操作:

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