Chris J. Lee

Dallas Drupal Developer

You are here

IE console.log fallback

Internet explorer doesn't have a console and thus it'll go nuts if you fire off commands like so:

console.log('I don't have a console!');

So therefore i found some neat fallbacks to fix this:

<!--[if lte IE 7]>
<SCRIPT LANGUAGE="javascript">
    (window.console = window.console || {}).log = function() { return window.alert.apply(window, arguments); };
</SCRIPT>
<![endif]-->

This will allow you to get fun window alerts to output console logs. Although, that could easily get annoying after a while.

So therefore twitter has a more robust solution. Try the following:

// 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;
        }
    }
}());

I didn't write any of these answers. I found them in an awesome SO thread.

Tags: 
jsie
© 2017 Chris J. Lee