120

Is there a console logger for IE? I'm trying to log a bunch of tests/assertions to the console but I can't do this in IE.

isherwood
  • 46,000
  • 15
  • 100
  • 132
ground5hark
  • 4,174
  • 7
  • 28
  • 35
  • 8
    watch out! `console.log()` only works when IE's dev tool is open (yes IE is crappy). see http://stackoverflow.com/questions/7742781/why-javascript-only-works-after-opening-developer-tools-in-ie-once – Adrien Be Jul 16 '13 at 13:15
  • 1
    use cross-browser wrapper: https://github.com/MichaelZelensky/log.js – Michael Zelensky Feb 18 '14 at 16:14

10 Answers10

149

You can access IE8 script console by launching the "Developer Tools" (F12). Click the "Script" tab, then click "Console" on the right.

From within your JavaScript code, you can do any of the following:

<script type="text/javascript">
    console.log('some msg');
    console.info('information');
    console.warn('some warning');
    console.error('some error');
    console.assert(false, 'YOU FAIL');
</script>

Also, you can clear the Console by calling console.clear().

NOTE: It appears you must launch the Developer Tools first then refresh your page for this to work.

Craig
  • 4,233
  • 2
  • 26
  • 32
  • This is only for IE8+, but it's a damn fine console. It's basically a replica of Firebug, missing a few features with some other ones thrown in. Search MSDN for it. – ken Apr 17 '10 at 02:52
  • 2
    Ah, even though it falls short of Firebug, it still has just enough of what I need. Thanks! – ground5hark Apr 17 '10 at 06:13
  • 1
    Link to [MSDN](http://msdn.microsoft.com/en-us/library/dd565625(v=vs.85).aspx#consolelogging) – Casebash Feb 24 '11 at 03:57
  • 73
    Please note that in IE, unlike in Firefox, if the developer tools are not active, window.console is undefined and calling `console.log()` will break. Always protect your calls with `window.console && console.log('stuff');` – Guss Apr 30 '12 at 12:32
  • 1
    In case anyone still can't find it, don't do what I did and try to type your script in the top portion of the window: the input area is actually a text area/bar at the bottom of the Developer Tools Console window. – starmandeluxe Jul 25 '13 at 19:20
24

Since version 8, Internet Explorer has its own console, like other browsers. However, if the console is not enabled, the console object does not exist and a call to console.log will throw an error.

Another option is to use log4javascript (full disclosure: written by me), which has its own logging console that works in all mainstream browsers, including IE >= 5, plus a wrapper for the browser's own console that avoids the issue of an undefined console.

Tim Down
  • 292,637
  • 67
  • 429
  • 506
14

Extremely important if using console.log() in production:

if you end up releasing console.log() commands to production you need to put in some kind of fix for IE - because console is only defined when in F12 debugging mode.

if (typeof console == "undefined") {
    this.console = { log: function (msg) { alert(msg); } };
}

[obviously remove the alert(msg); statement once you've verified it works]

See also 'console' is undefined error for Internet Explorer for other solutions and more details

Community
  • 1
  • 1
Simon_Weaver
  • 120,240
  • 73
  • 577
  • 618
9

There is Firebug Lite which gives a lot of Firebug functionality in IE.

Daniel DiPaolo
  • 51,147
  • 13
  • 111
  • 112
5

Simple IE7 and below shim that preserves Line Numbering for other browsers:

/* console shim*/
(function () {
    var f = function () {};
    if (!window.console) {
        window.console = {
            log:f, info:f, warn:f, debug:f, error:f
        };
    }
}());
dbrin
  • 15,197
  • 4
  • 51
  • 82
3

In his book, "Secrets of Javascript Ninja", John Resig (creator of jQuery) has a really simple code which will handle cross-browser console.log issues. He explains that he would like to have a log message which works with all browsers and here is how he coded it:

function log() {
  try {
    console.log.apply(console, arguments);
  } catch(e) {
  try {
    opera.postError.apply(opera, arguments);
  }
  catch(e) {
    alert(Array.prototype.join.call( arguments, " "));
  }
}
ambodi
  • 5,445
  • 2
  • 30
  • 21
2

For IE8 or console support limited to console.log (no debug, trace, ...) you can do the following:

  • If console OR console.log undefined: Create dummy functions for console functions (trace, debug, log, ...)

    window.console = { debug : function() {}, ...};

  • Else if console.log is defined (IE8) AND console.debug (any other) is not defined: redirect all logging functions to console.log, this allows to keep those logs !

    window.console = { debug : window.console.log, ...};

Not sure about the assert support in various IE versions, but any suggestions are welcome.

Christophe Roussy
  • 13,732
  • 2
  • 75
  • 75
0

You can use cross-browser wrapper: https://github.com/MichaelZelensky/log.js

Michael Zelensky
  • 1,781
  • 3
  • 22
  • 34
0

For older version of IE (before IE8), it is not straight forward to see the console log in IE Developer Toolbar, after spending hours research and trying many different solutions, finally, the following toolbar is great tool for me:

The main advantage of this is providing a console for IE6 or IE7, so you can see what are the error (in the console log)

  • Note:
  • It is free
  • screen shot of the toolbar

enter image description here

super1ha1
  • 549
  • 1
  • 7
  • 15
0

I've been always doing something like this:

var log = (function () {
  try {
    return console.log;
  }
  catch (e) {
    return function () {};
  }
}());

and from that point just always use log(...), don't be too fancy using console.[warn|error|and so on], just keep it simple. I usually prefer simple solution then fancy external libraries, it usually pays off.

simple way to avoid problems with IE (with non existing console.log)

stopsopa
  • 103
  • 1
  • 13