čtvrtek 15. května 2014

Simple console.log wrapper hangs IE10 and Chrome

Many programmers write a wrapper around console.log to use logging only when it's available. I have reassigned log to my own variable this.log = console.log; and it has worked fine in Firefox with Firebug plugin. However, it has caused Javascript to stop working in Internet Explorer 10 (IE10) and Chrome. Strange thing is, IE10 hangs only when it has closed the developer tools F12. When I have opened the developer tools to see what's going on, the IE10 has proceeded well.

var mylog = {
    log : this._logNull,
    init: function() {
        if (typeof console == 'object' && typeof console.log == 'function') {
            console.log(this);
            this.log = console.log; // IE10 hangs bug
            //this.log = this._log; // IE10 ok
        }
    },
    _logNull : function() {},
    _log : function() { console.log.apply(console, arguments); }
    
};
Try the code at JSFiddle.

After writing a proper wrapper function _log, the code goes well even in IE10 with closed developer tools.

Note, this is just proof of concept code for IE10. The real console.log wrapper may become more complicated when considering various browser like Chrome, Safari, etc.