lyuehh
4/24/2012 - 3:44 AM

console.log for ie

console.log for ie

/* Faux Console by Chris Heilmann http://wait-till-i.com */
var ie = function() {
        var v = 4,
            //原作者的此处代码是3,考虑了IE5的情况,我改为4。
            div = document.createElement('div'),
            i = div.getElementsByTagName('i');
        do {
            div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->';
        } while (i[0]);
        return v > 5 ? v : false; //如果不是IE,之前返回undefined,现改为返回false。
    }();

if (ie < 9) {
    var console = {
        init: function() {
            console.d = document.createElement('div');
            document.body.appendChild(console.d);
            var a = document.createElement('a');
            a.href = 'javascript:console.hide()';
            a.innerHTML = 'close';
            console.d.appendChild(a);
            var a = document.createElement('a');
            a.href = 'javascript:console.clear();';
            a.innerHTML = 'clear';
            console.d.appendChild(a);
            a.style.cssText = "float:right;padding-left:1em;padding-bottom:.5em;text-align:right;";
            var id = 'fauxconsole';
            if (!document.getElementById(id)) {
                console.d.id = id;
                console.d.style.cssText = "position:absolute;top:0;right:0;width:300px;border:1px solid #999;font-family:courier,monospace;background:#eee;font-size:10px;padding:10px;";
            }
            console.show();
            alert(123);
        },
        hide: function() {
            console.d.style.display = 'none';
        },
        show: function() {
            console.d.style.display = 'block';
        },
        log: function(o) {
            console.d.innerHTML += '<br/>' + o;
            console.show();
        },
        clear: function() {
            console.d.parentNode.removeChild(console.d);
            console.init();
            console.show();
        },
        /*Simon Willison rules*/
        addLoadEvent: function(func) {
            var oldonload = window.onload;
            if (typeof window.onload != 'function') {
                window.onload = func;
            } else {
                window.onload = function() {
                    if (oldonload) {
                        oldonload();
                    }
                    func();
                }
            };
        }
    };
    console.addLoadEvent(console.init);
}