Javascript IE9 是否支持console.log,是否真的有功能?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5472938/
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
Does IE9 support console.log, and is it a real function?
提问by mloughran
In which circumstances is window.console.log
defined in Internet Explorer 9?
window.console.log
Internet Explorer 9 中定义了哪些情况?
Even when window.console.log
is defined, window.console.log.apply
and window.console.log.call
are undefined. Why is this?
即使window.console.log
被定义,window.console.log.apply
并且window.console.log.call
是不确定的。为什么是这样?
[Related question for IE8: What happened to console.log in IE8?.]
【IE8相关问题:IE8中的console.log是怎么回事?.]
回答by Andy E
In Internet Explorer 9 (and 8), the console
object is only exposed when the developer tools are opened for a particular tab. If you hide the developer tools window for that tab, the console
object remains exposed for each page you navigate to. If you open a new tab, you must also open the developer tools for that tab in order for the console
object to be exposed.
在 Internet Explorer 9(和 8)中,console
只有在为特定选项卡打开开发人员工具时才会公开该对象。如果您隐藏该选项卡的开发人员工具窗口,则console
您导航到的每个页面的对象都会保持公开。如果您打开一个新选项卡,您还必须打开该选项卡的开发人员工具,以便console
公开对象。
The console
object is not part of any standard and is an extension to the Document Object Model. Like other DOM objects, it is considered a host object and is not required to inherit from Object
, nor its methods from Function
, like native ECMAScript functions and objects do. This is the reason apply
and call
are undefined on those methods. In IE 9, most DOM objects were improved to inherit from native ECMAScript types. As the developer tools are considered an extension to IE (albeit, a built-in extension), they clearly didn't receive the same improvements as the rest of the DOM.
该console
对象不是任何标准的一部分,而是文档对象模型的扩展。像其他 DOM 对象一样,它被认为是宿主对象,不需要像原生 ECMAScript 函数和对象那样继承自Object
,也不需要继承自的方法Function
。这就是原因apply
并且call
在这些方法上未定义。在 IE 9 中,大多数 DOM 对象都被改进为继承自原生 ECMAScript 类型。由于开发人员工具被认为是 IE 的扩展(尽管是内置扩展),因此它们显然没有像 DOM 的其余部分那样得到相同的改进。
For what it's worth, you can still use some Function.prototype
methods on console
methods with a little bind()
magic:
对于它的价值,您仍然可以在Function.prototype
方法上使用一些console
具有一点bind()
魔力的方法:
var log = Function.prototype.bind.call(console.log, console);
log.apply(console, ["this", "is", "a", "test"]);
//-> "thisisatest"
回答by Michael Erickson
A simple solution to this console.log problem is to define the following at the beginning of your JS code:
这个 console.log 问题的一个简单解决方案是在 JS 代码的开头定义以下内容:
if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function () { };
This works for me in all browsers. This creates a dummy function for console.log when the debugger is not active. When the debugger is active, the method console.log is defined and executes normally.
这在所有浏览器中都适用于我。当调试器不活动时,这会为 console.log 创建一个虚拟函数。当调试器处于活动状态时,方法 console.log 被定义并正常执行。
回答by Steven Anderson
I know this is a very old question but feel this adds a valuable alternative of how to deal with the console issue. Place the following code before any call to console.* (so your very first script).
我知道这是一个非常古老的问题,但我觉得这为如何处理控制台问题增加了一个有价值的替代方案。在对 console.* 的任何调用之前放置以下代码(这是您的第一个脚本)。
// Avoid `console` errors in browsers that lack a console.
(function() {
var method;
var noop = function () {};
var methods = [
'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
'exception', 'group', 'groupCollapsed', 'groupEnd', 'info', 'log',
'markTimeline', 'profile', 'profileEnd', 'table', 'time', 'timeEnd',
'timeStamp', 'trace', 'warn'
];
var length = methods.length;
var console = (window.console = window.console || {});
while (length--) {
method = methods[length];
// Only stub undefined methods.
if (!console[method]) {
console[method] = noop;
}
}
}());
Reference:
https://github.com/h5bp/html5-boilerplate/blob/v5.0.0/dist/js/plugins.js
参考:https :
//github.com/h5bp/html5-boilerplate/blob/v5.0.0/dist/js/plugins.js
回答by John
console.log is only defined when the console is open. If you want to check for it in your code make sure you check for for it within the window property
console.log 仅在控制台打开时定义。如果您想在代码中检查它,请确保在 window 属性中检查它
if (window.console)
console.log(msg)
this throws an exception in IE9 and will not work correctly. Do not do this
这会在 IE9 中引发异常并且无法正常工作。不要这样做
if (console)
console.log(msg)
回答by James Ford
After reading the article from Marc Cliament's comment above, I've now changed my all-purpose cross-browser console.log function to look like this:
在阅读了上面 Marc Cliament 评论中的文章后,我现在将我的通用跨浏览器 console.log 函数更改为如下所示:
function log()
{
"use strict";
if (typeof(console) !== "undefined" && console.log !== undefined)
{
try
{
console.log.apply(console, arguments);
}
catch (e)
{
var log = Function.prototype.bind.call(console.log, console);
log.apply(console, arguments);
}
}
}
回答by Paolo Mioni
I would like to mention that IE9 does not raise the error if you use console.log with developer tools closed on all versions of Windows. On XP it does, but on Windows 7 it doesn't. So if you dropped support for WinXP in general, you're fine using console.log directly.
我想提一下,如果您使用 console.log 并在所有版本的 Windows 上关闭开发人员工具,IE9 不会引发错误。在 XP 上是这样,但在 Windows 7 上则不是。因此,如果您总体上放弃了对 WinXP 的支持,则可以直接使用 console.log。
回答by Ceilingfish
How about...
怎么样...
console = { log : function(text) { alert(text); } }