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

7

213

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?.]

Shopping answered 29/3, 2011 at 13:3 Comment(4)
Check out this great post about the intricacies of IE8-9 console object/function: whattheheadsaid.com/2011/04/…Miscible
See also 'console' is undefined error for internet explorerPutup
@MarcCliment the link is deadAudwen
@Audwen I hate when this happens, there's the link from the web archive: web.archive.org/web/20140625085155/http://whattheheadsaid.com/…Miscible
N
302

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"
Nootka answered 29/3, 2011 at 13:22 Comment(5)
The same is true for Firebug's console object.Mediate
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!!Hexylresorcinol
Had 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).Binns
you should have checked error log at the very first time the problem came to you on IE @HexylresorcinolSarson
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.Flicker
T
168

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.

Tuyere answered 6/3, 2012 at 23:56 Comment(3)
More info, and more robust console replacements (including other console methods) here: #8002616Unification
@ZachL: Which ones in concrete precisely?Madelle
My answer has one approach: https://mcmap.net/q/21527/-should-i-be-removing-console-log-from-production-code. Also check this out: github.com/paulmillr/console-polyfill/blob/master/index.jsUnification
T
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

Tumor answered 6/8, 2014 at 1:20 Comment(0)
M
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)
Marshy answered 5/2, 2013 at 18:15 Comment(0)
L
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);
        }
    }
}
Libel answered 18/10, 2012 at 11:29 Comment(2)
Just Function.prototype.apply.call(console.log, console, arguments);Eudoxia
@Eudoxia it definitely should be the only one accepted answer!Quarrier
A
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.

Afield answered 12/10, 2016 at 10:50 Comment(0)
T
-1

How about...

console = { log : function(text) { alert(text); } }
Talipes answered 31/7, 2012 at 10:33 Comment(1)
This might be a passable workaround in some cases, but you haven't actually addressed the question.Barbarese

© 2022 - 2024 — McMap. All rights reserved.