Does IE9 Support Console.Log, and Is It a Real Function

Does IE9 support console.log, and is it a real function?

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.

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.

For what it's worth, you can still use some Function.prototype methods on console methods with a little bind() magic:

var log = Function.prototype.bind.call(console.log, console);
log.apply(console, ["this", "is", "a", "test"]);
//-> "thisisatest"

console.log.apply not working in IE9

The second part of an answer I gave recently answers this question too. I don't consider this a duplicate of that one so, for convenience, I'll paste it here:

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.

For what it's worth, you can still use some Function.prototype methods on console methods with a little bind() magic:

var log = Function.prototype.bind.call(console.log, console);
log.apply(console, ["this", "is", "a", "test"]);
//-> "thisisatest"

So you could fix up all the console methods for IE 9 in the same manner:

if (Function.prototype.bind && window.console && typeof console.log == "object"){
[
"log","info","warn","error","assert","dir","clear","profile","profileEnd"
].forEach(function (method) {
console[method] = this.bind(console[method], console);
}, Function.prototype.call);
}

This replaces the "host" functions with native functions that call the "host" functions. You can get it working in Internet Explorer 8 by including the compatibility implementations for Function.prototype.bind and Array.prototype.forEach in your code, or rewriting the above snippet to incorporate the techniques used by those methods.

See also

  • console.log typeof is "object" instead of "function" - Microsoft Connect (Live account required)

Anybody know why IE9 typeof console.log reports object , others report function ?

It seems to be a bug in IE, as many (or all) console elements that should be functions appear to be objects instead.

If you're trying to call function methods that aren't there, then you might want to refer to this article:
http://whattheheadsaid.com/2011/04/internet-explorer-9s-problematic-console-object

Otherwise the simplest solution is do:

typeof(console.log) !== 'undefined'

It's not the prettiest solution as it's really a bug with IE failing standards compliance in spite of their drive to do the opposite, but console.log shouldn't really be anything other than an object or function so it should be safe to use. Otherwise you could do something more complex like:

switch (typeof(console.log)) {
case 'object':
case 'function':
// Should be a valid console.log object, do something with it
break;
}

Console.log IE9 issue

If you want to use console.log() and have it not bomb out in IE when the IE debugger is not running, you can place the following in your javascript at the global scope before any console.log() statements execute to give you a dummy console.log() that will keep your console.log() statements from causing errors:

if (!window.console) {window.console = {};}
if (!console.log) {console.log = function() {};}

Of course, if you actually want to see the console.log() output in IE, then you will have to run the IE debugger which will cause console.log() to get defined or use some other debugging environment that defines it.

Why can't I bind directly console.log on IE9 with developer tools open?

As the related answer says, it is simply because the log function from the console object in IE doesn't inherit from Function. It's a host object, and it uses whatever rules IE sees fit.

But it's a function-like. That's why using Function.prototype.bind works, just like using Array.prototype.forEach works on array-like objects. (Hint: NodeLists and HTMLCollections.)

It's not a bug per se, because there is no specification talking about the console object. The DOM living standard doesn't even mention it. So each browser implements this object the way it wants to.

And it does mean that the window.alert function is subject to the same problems. We're lucky that it works so well across browsers.

That's IE. Deal with it. Although IE9 is far better than IE8, it's still way worse than the other modern browsers.

Getting logs in Internet Explorer 8 and 9 without opening developer tool view

window.console = window.console || {log: function(){}};

THis wont let you access the logs, but it will avoid null pointer errors.

Website breaks in IE9 but works when Developer Tools is opened

Do you have any console.log() in your code? IE < 9 does not expose the console object unless the developer tool is open. For further details see
Does IE9 support console.log, and is it a real function?

Cheers



Related Topics



Leave a reply



Submit