Does IE9 support console.log, and is it a real function?
Solution 1
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"
Solution 2
A simple solution to this console.log problem is to define the following at the beginning of your JS code:
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.
Solution 3
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).
// 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
Solution 4
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
if (window.console)
console.log(msg)
this throws an exception in IE9 and will not work correctly. Do not do this
if (console)
console.log(msg)
Solution 5
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:
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);
}
}
}
Related videos on Youtube
mloughran
Updated on July 08, 2022Comments
-
mloughran almost 2 years
In which circumstances is
window.console.log
defined in Internet Explorer 9?Even when
window.console.log
is defined,window.console.log.apply
andwindow.console.log.call
are undefined. Why is this?[Related question for IE8: What happened to console.log in IE8?.]
-
Marc Climent over 12 yearsCheck out this great post about the intricacies of IE8-9 console object/function: whattheheadsaid.com/2011/04/…
-
Bergi over 11 years
-
chakeda almost 6 years@MarcCliment the link is dead
-
Marc Climent almost 6 years@chakeda I hate when this happens, there's the link from the web archive: web.archive.org/web/20140625085155/http://whattheheadsaid.com/…
-
-
Marcel Korpel about 13 yearsThe same is true for Firebug's
console
object. -
f055 almost 12 yearsI can un-proudly say that for the many years I developed for the web I assumed console.log is supported by all major browsers. I just spent a day working out why IE9 doesn't like my script and now I know why - it had a console.log in the very first step. Impossible to debug, since turning debug mode made this bug go away in an instant :P Thanks for clarification!!
-
Simon A. Eugster over 11 yearsHad the same problem yesterday. Installing DebugBar happened to help me quicklier since it does not define the console object. So when I hid the IE console but not the DebugBar I got a message from latter that there was a JavaScript error (console is not defined).
-
Zach Lysobey almost 11 yearsMore info, and more robust console replacements (including other console methods) here: stackoverflow.com/questions/8002116/…
-
Victor over 10 yearsJust
Function.prototype.apply.call(console.log, console, arguments);
-
Lucky Ali over 10 yearsyou should have checked error log at the very first time the problem came to you on IE @f055
-
Seth Flowers about 10 yearsInternet Options -> Advanced -> Display a notification about every script error. Web developers should always leave this checked in IE. This would've informed you about console, or the log function being undefined... can't remember the message exactly.
-
p.s.w.g over 9 yearsThis might be a passable workaround in some cases, but you haven't actually addressed the question.
-
Pavel Frankov about 9 years@Victor it definitely should be the only one accepted answer!
-
hakre over 8 years@ZachL: Which ones in concrete precisely?
-
Zach Lysobey over 8 yearsMy answer has one approach: stackoverflow.com/a/15771110/363701. Also check this out: github.com/paulmillr/console-polyfill/blob/master/index.js