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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2656730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:20:37  来源:igfitidea点击:

How can I use console logging in Internet Explorer?

javascriptinternet-explorerinternet-explorer-8consolefirebug

提问by ground5hark

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 中执行此操作。

回答by Craig

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.

注意:看来您必须先启动开发人员工具,然后刷新页面才能使其正常工作。

回答by Tim Down

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

从版本 8 开始,Internet Explorer 拥有自己的控制台,就像其他浏览器一样。但是,如果未启用控制台,则console对象不存在并且调用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,以及浏览器自己的控制台的包装器,避免了 undefined 的问题console

回答by Simon_Weaver

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

如果在生产中使用 console.log() 非常重要:

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

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

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

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

[显然删除警报(味精); 一旦你验证它有效的声明]

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

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

回答by Daniel DiPaolo

There is Firebug Litewhich gives a lot of Firebug functionality in IE.

Firebug的精简版这给了很多的IE Firebug的功能。

回答by dbrin

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
        };
    }
}());

回答by ambodi

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, " "));
  }
}

回答by Christophe Roussy

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, ...)

    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 !

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

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

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

  • 否则,如果console.log 已定义(IE8)且console.debug(任何其他)未定义:将所有日志功能重定向到console.log,这允许保留这些日志!

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

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

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

回答by Michael Zelensky

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

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

回答by super1ha1

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 开发者工具栏中看到控制台登录并不是很直接,经过几个小时的研究和尝试了许多不同的解决方案,最后,以下工具栏对我来说是一个很好的工具:

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
  • 笔记:
  • 这是免费的
  • 工具栏的屏幕截图

enter image description here

在此处输入图片说明