210

In which circumstances is window.console.log defined in Internet Explorer 9?

Even when window.console.log is defined, window.console.log.apply and window.console.log.call are undefined. Why is this?

[Related question for IE8: What happened to console.log in IE8?.]

Community
  • 1
  • 1
mloughran
  • 10,067
  • 7
  • 26
  • 21
  • 3
    Check out this great post about the intricacies of IE8-9 console object/function: http://whattheheadsaid.com/2011/04/internet-explorer-9s-problematic-console-object – Marc Climent Dec 30 '11 at 13:37
  • See also ['console' is undefined error for internet explorer](http://stackoverflow.com/q/3326650/1048572) – Bergi Nov 10 '12 at 12:34
  • @MarcCliment the link is dead – chakeda Apr 27 '18 at 15:53
  • @chakeda I hate when this happens, there's the link from the web archive: https://web.archive.org/web/20140625085155/http://whattheheadsaid.com/2011/04/internet-explorer-9s-problematic-console-object – Marc Climent Apr 29 '18 at 21:25

7 Answers7

300

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"
Andy E
  • 311,406
  • 78
  • 462
  • 440
  • 2
    The same is true for Firebug's `console` object. – Marcel Korpel Mar 29 '11 at 13:36
  • 151
    I 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!! – f055 Jul 16 '12 at 22:00
  • 2
    Had the same problem yesterday. Installing [DebugBar](http://www.debugbar.com/) 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). – Simon A. Eugster Sep 28 '12 at 05:21
  • you should have checked error log at the very first time the problem came to you on IE @f055 – Lucky Ali Jan 17 '14 at 05:21
  • 7
    Internet 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. – Seth Flowers Feb 07 '14 at 19:20
167

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.

Michael Erickson
  • 3,341
  • 1
  • 17
  • 9
  • 8
    More info, and more robust console replacements (including other console methods) here: http://stackoverflow.com/questions/8002116/should-i-be-removing-console-log-from-production-code/15771110 – Zach Lysobey Jun 13 '13 at 19:15
  • @ZachL: Which ones in concrete precisely? – hakre Aug 11 '15 at 08:41
  • My answer has one approach: http://stackoverflow.com/a/15771110/363701. Also check this out: https://github.com/paulmillr/console-polyfill/blob/master/index.js – Zach Lysobey Aug 11 '15 at 14:09
14

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

Steven Anderson
  • 7,810
  • 4
  • 22
  • 32
10

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)
Thomas Bormans
  • 4,558
  • 5
  • 30
  • 46
John
  • 126
  • 1
  • 6
6

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);
        }
    }
}
James Ford
  • 1,483
  • 2
  • 14
  • 31
0

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.

Paolo Mioni
  • 990
  • 8
  • 17
-1

How about...

console = { log : function(text) { alert(text); } }
Ceilingfish
  • 5,131
  • 3
  • 41
  • 68
  • 1
    This might be a passable workaround in some cases, but you haven't actually addressed the question. – p.s.w.g Jan 19 '15 at 21:49