如何在Internet Explorer中使用控制台日志记录?

时间:2022-10-24 20:47:12

Is there a console logger for IE? I'm trying to log a bunch of tests/assertions to the console but I can't do this in IE.

IE是否有控制台记录器?我正在尝试将一堆测试/断言记录到控制台,但我不能在IE中执行此操作。

9 个解决方案

#1


140  

You can access IE8 script console by launching the "Developer Tools" (F12). Click the "Script" tab, then click "Console" on the right.

您可以通过启动“开发人员工具”(F12)来访问IE8脚本控制台。单击“脚本”选项卡,然后单击右侧的“控制台”。

From within your JavaScript code, you can do any of the following:

在JavaScript代码中,您可以执行以下任何操作:

<script type="text/javascript">
    console.log('some msg');
    console.info('information');
    console.warn('some warning');
    console.error('some error');
    console.assert(false, 'YOU FAIL');
</script>

Also, you can clear the Console by calling console.clear().

此外,您可以通过调用console.clear()清除控制台。

NOTE: It appears you must launch the Developer Tools first then refresh your page for this to work.

注意:您必须首先启动开发人员工具,然后刷新页面才能使用此工具。

#2


22  

Since version 8, Internet Explorer has its own console, like other browsers. However, if the console is not enabled, the console object does not exist and a call to console.log will throw an error.

从版本8开始,Internet Explorer就像其他浏览器一样拥有自己的控制台。但是,如果未启用控制台,则控制台对象不存在,并且对console.log的调用将引发错误。

Another option is to use log4javascript (full disclosure: written by me), which has its own logging console that works in all mainstream browsers, including IE >= 5, plus a wrapper for the browser's own console that avoids the issue of an undefined console.

另一种选择是使用log4javascript(完全披露:由我编写),它有自己的日志记录控制台,适用于所有主流浏览器,包括IE> = 5,加上浏览器自己的控制台的包装器,避免了未定义控制台的问题。

#3


11  

Extremely important if using console.log() in production:

if you end up releasing console.log() commands to production you need to put in some kind of fix for IE - because console is only defined when in F12 debugging mode.

如果您最终将console.log()命令发布到生产中,则需要为IE进行某种修复 - 因为控制台仅在F12调试模式下定义。

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

[obviously remove the alert(msg); statement once you've verified it works]

[明显删除警报(msg);声明一旦你验证它的工作原理]

See also 'console' is undefined error for Internet Explorer for other solutions and more details

另请参阅“控制台”是Internet Explorer的其他解决方案的未定义错误和更多详细信息

#4


9  

There is Firebug Lite which gives a lot of Firebug functionality in IE.

Firebug Lite在IE中提供了很多Firebug功能。

#5


4  

Simple IE7 and below shim that preserves Line Numbering for other browsers:

简单的IE7及以下垫片,为其他浏览器保留行号:

/* console shim*/
(function () {
    var f = function () {};
    if (!window.console) {
        window.console = {
            log:f, info:f, warn:f, debug:f, error:f
        };
    }
}());

#6


3  

In his book, "Secrets of Javascript Ninja", John Resig (creator of jQuery) has a really simple code which will handle cross-browser console.log issues. He explains that he would like to have a log message which works with all browsers and here is how he coded it:

在他的书“Javascript Ninja的秘密”中,John Resig(jQuery的创建者)有一个非常简单的代码,可以处理跨浏览器的console.log问题。他解释说,他希望有一条适用于所有浏览器的日志消息,以下是他编码的方式:

function log() {
  try {
    console.log.apply(console, arguments);
  } catch(e) {
  try {
    opera.postError.apply(opera, arguments);
  }
  catch(e) {
    alert(Array.prototype.join.call( arguments, " "));
  }
}

#7


2  

For IE8 or console support limited to console.log (no debug, trace, ...) you can do the following:

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

  • If console OR console.log undefined: Create dummy functions for console functions (trace, debug, log, ...)

    如果console OR console.log undefined:为控制台函数创建虚函数(trace,debug,log,...)

    window.console = { debug : function() {}, ...};

    window.console = {debug:function(){},...};

  • Else if console.log is defined (IE8) AND console.debug (any other) is not defined: redirect all logging functions to console.log, this allows to keep those logs !

    否则,如果定义了console.log(IE8)并且未定义console.debug(任何其他):将所有日志记录功能重定向到console.log,这样可以保留这些日志!

    window.console = { debug : window.console.log, ...};

    window.console = {debug:window.console.log,...};

Not sure about the assert support in various IE versions, but any suggestions are welcome.

不确定各种IE版本中的断言支持,但欢迎任何建议。

#8


0  

You can use cross-browser wrapper: https://github.com/MichaelZelensky/log.js

您可以使用跨浏览器包装器:https://github.com/MichaelZelensky/log.js

#9


0  

For older version of IE (before IE8), it is not straight forward to see the console log in IE Developer Toolbar, after spending hours research and trying many different solutions, finally, the following toolbar is great tool for me:

对于IE的旧版本(在IE8之前),在花费数小时研究并尝试许多不同的解决方案之后,在IE Developer Toolbar中看到控制台日志并不是直接的,最后,以下工具栏对我来说是很好的工具:

The main advantage of this is providing a console for IE6 or IE7, so you can see what are the error (in the console log)

这样做的主要优点是为IE6或IE7提供了一个控制台,因此您可以看到错误是什么(在控制台日志中)

  • Note:
  • 注意:
  • It is free
  • 这是免费的
  • screen shot of the toolbar
  • 屏幕截图的工具栏

如何在Internet Explorer中使用控制台日志记录?

#1


140  

You can access IE8 script console by launching the "Developer Tools" (F12). Click the "Script" tab, then click "Console" on the right.

您可以通过启动“开发人员工具”(F12)来访问IE8脚本控制台。单击“脚本”选项卡,然后单击右侧的“控制台”。

From within your JavaScript code, you can do any of the following:

在JavaScript代码中,您可以执行以下任何操作:

<script type="text/javascript">
    console.log('some msg');
    console.info('information');
    console.warn('some warning');
    console.error('some error');
    console.assert(false, 'YOU FAIL');
</script>

Also, you can clear the Console by calling console.clear().

此外,您可以通过调用console.clear()清除控制台。

NOTE: It appears you must launch the Developer Tools first then refresh your page for this to work.

注意:您必须首先启动开发人员工具,然后刷新页面才能使用此工具。

#2


22  

Since version 8, Internet Explorer has its own console, like other browsers. However, if the console is not enabled, the console object does not exist and a call to console.log will throw an error.

从版本8开始,Internet Explorer就像其他浏览器一样拥有自己的控制台。但是,如果未启用控制台,则控制台对象不存在,并且对console.log的调用将引发错误。

Another option is to use log4javascript (full disclosure: written by me), which has its own logging console that works in all mainstream browsers, including IE >= 5, plus a wrapper for the browser's own console that avoids the issue of an undefined console.

另一种选择是使用log4javascript(完全披露:由我编写),它有自己的日志记录控制台,适用于所有主流浏览器,包括IE> = 5,加上浏览器自己的控制台的包装器,避免了未定义控制台的问题。

#3


11  

Extremely important if using console.log() in production:

if you end up releasing console.log() commands to production you need to put in some kind of fix for IE - because console is only defined when in F12 debugging mode.

如果您最终将console.log()命令发布到生产中,则需要为IE进行某种修复 - 因为控制台仅在F12调试模式下定义。

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

[obviously remove the alert(msg); statement once you've verified it works]

[明显删除警报(msg);声明一旦你验证它的工作原理]

See also 'console' is undefined error for Internet Explorer for other solutions and more details

另请参阅“控制台”是Internet Explorer的其他解决方案的未定义错误和更多详细信息

#4


9  

There is Firebug Lite which gives a lot of Firebug functionality in IE.

Firebug Lite在IE中提供了很多Firebug功能。

#5


4  

Simple IE7 and below shim that preserves Line Numbering for other browsers:

简单的IE7及以下垫片,为其他浏览器保留行号:

/* console shim*/
(function () {
    var f = function () {};
    if (!window.console) {
        window.console = {
            log:f, info:f, warn:f, debug:f, error:f
        };
    }
}());

#6


3  

In his book, "Secrets of Javascript Ninja", John Resig (creator of jQuery) has a really simple code which will handle cross-browser console.log issues. He explains that he would like to have a log message which works with all browsers and here is how he coded it:

在他的书“Javascript Ninja的秘密”中,John Resig(jQuery的创建者)有一个非常简单的代码,可以处理跨浏览器的console.log问题。他解释说,他希望有一条适用于所有浏览器的日志消息,以下是他编码的方式:

function log() {
  try {
    console.log.apply(console, arguments);
  } catch(e) {
  try {
    opera.postError.apply(opera, arguments);
  }
  catch(e) {
    alert(Array.prototype.join.call( arguments, " "));
  }
}

#7


2  

For IE8 or console support limited to console.log (no debug, trace, ...) you can do the following:

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

  • If console OR console.log undefined: Create dummy functions for console functions (trace, debug, log, ...)

    如果console OR console.log undefined:为控制台函数创建虚函数(trace,debug,log,...)

    window.console = { debug : function() {}, ...};

    window.console = {debug:function(){},...};

  • Else if console.log is defined (IE8) AND console.debug (any other) is not defined: redirect all logging functions to console.log, this allows to keep those logs !

    否则,如果定义了console.log(IE8)并且未定义console.debug(任何其他):将所有日志记录功能重定向到console.log,这样可以保留这些日志!

    window.console = { debug : window.console.log, ...};

    window.console = {debug:window.console.log,...};

Not sure about the assert support in various IE versions, but any suggestions are welcome.

不确定各种IE版本中的断言支持,但欢迎任何建议。

#8


0  

You can use cross-browser wrapper: https://github.com/MichaelZelensky/log.js

您可以使用跨浏览器包装器:https://github.com/MichaelZelensky/log.js

#9


0  

For older version of IE (before IE8), it is not straight forward to see the console log in IE Developer Toolbar, after spending hours research and trying many different solutions, finally, the following toolbar is great tool for me:

对于IE的旧版本(在IE8之前),在花费数小时研究并尝试许多不同的解决方案之后,在IE Developer Toolbar中看到控制台日志并不是直接的,最后,以下工具栏对我来说是很好的工具:

The main advantage of this is providing a console for IE6 or IE7, so you can see what are the error (in the console log)

这样做的主要优点是为IE6或IE7提供了一个控制台,因此您可以看到错误是什么(在控制台日志中)

  • Note:
  • 注意:
  • It is free
  • 这是免费的
  • screen shot of the toolbar
  • 屏幕截图的工具栏

如何在Internet Explorer中使用控制台日志记录?