在哪些情况下,在Internet Explorer 9中定义了window.console.log
?IE9是否支持console.log,它是一个真正的功能?
即使定义了window.console.log
,window.console.log.apply
和window.console.log.call
也未定义。为什么是这样?
[用于IE8相关问题:What happened to console.log in IE8?]
在哪些情况下,在Internet Explorer 9中定义了window.console.log
?IE9是否支持console.log,它是一个真正的功能?
即使定义了window.console.log
,window.console.log.apply
和window.console.log.call
也未定义。为什么是这样?
[用于IE8相关问题:What happened to console.log in IE8?]
在Internet Explorer 9(和图8),当显影剂工具打开,以便一个特定标签的console
对象仅暴露。如果您隐藏该选项卡的开发人员工具窗口,则对于您导航到的每个页面,console
对象都将保持公开。如果您打开一个新选项卡,则还必须打开该选项卡的开发人员工具才能显示console
对象。
console
对象不是任何标准的一部分,并且是文档对象模型的扩展。像其他DOM对象一样,它被认为是主机对象,并不需要像本地ECMAScript函数和对象那样继承Object
,也不需要从Function
继承它的方法。这就是在这些方法上未定义apply
和call
的原因。在IE 9中,大多数DOM对象都得到了改进,可以从本机ECMAScript类型继承。由于开发人员工具被认为是IE的扩展(虽然是内置扩展),但他们显然没有得到与DOM其余部分相同的改进。
对于它的价值,你仍然可以使用上console
方法的一些Function.prototype
方法有一点bind()
法宝:
var log = Function.prototype.bind.call(console.log, console);
log.apply(console, ["this", "is", "a", "test"]);
//-> "thisisatest"
对于Firebug的'console'对象也是如此。 – 2011-03-29 13:36:43
我可以不自豪地说,我为web开发多年,我认为所有主流浏览器都支持console.log。我花了一天时间研究为什么IE9不喜欢我的脚本,现在我知道为什么 - 它在第一步中有一个console.log。无法调试,因为转向调试模式使这个错误瞬间消失:P谢谢澄清! – f055 2012-07-16 22:00:01
昨天有同样的问题。安装[DebugBar](http://www.debugbar.com/)碰巧帮助我更快,因为它没有定义控制台对象。所以,当我隐藏IE控制台而不是DebugBar时,我从后者得到一条消息,说明存在JavaScript错误(控制台未定义)。 – 2012-09-28 05:21:46
一个简单的解决这个问题的console.log是确定之初以下你的JS代码:
if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function() { };
这适用于所有浏览器。这会在调试器未处于活动状态时为console.log创建一个虚拟函数。当调试器处于活动状态时,将定义方法console.log并正常执行。
......怎么
console = { log : function(text) { alert(text); } }
在某些情况下,这可能是一个可以通过的解决方法,但实际上并未解决该问题。 – 2015-01-19 21:49:57
阅读从上面马克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);
}
}
}
只是'Function.prototype.apply.call(console.log,控制台,参数);' – Victor 2013-10-29 09:52:33
@Victor它绝对应该是唯一一个被接受的答案! – 2015-03-11 12:41:10
console.log仅在控制台打开时定义。如果你想在你的代码来检查它确保您的窗口属性
if (window.console)
console.log(msg)
,这将引发一个例外IE9,将无法正常工作范围内检查它。不要这样做
if (console)
console.log(msg)
我知道这是一个非常古老的问题,但感觉这增加了如何处理控制台问题的有价值的选择。在调用控制台之前放置以下代码*(这是您的第一个脚本)。
// 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;
}
}
}());
参考:
https://github.com/h5bp/html5-boilerplate/blob/v5.0.0/dist/js/plugins.js
我想提一提,如果你使用的console.log与开发工具的所有版本的Windows关闭IE9不会引发错误。在XP上它确实,但在Windows 7上却没有。 所以,如果你放弃了对WinXP的支持,你可以直接使用console.log。
不正确仍然是Windows 7上的问题 – 2017-03-08 21:15:35
查看关于IE8-9控制台对象/函数错综复杂的好帖子:http://whattheheadsaid.com/2011/04/internet-explorer-9s-problematic-console-object – 2011-12-30 13:37:51
另请参阅['console'是Internet Explorer的未定义错误](http://stackoverflow.com/q/3326650/1048572) – Bergi 2012-11-10 12:34:03