IE6789浏览器使用console.log类似的方法输出调试内容但又不影响页面正常运行

专门为ie下调试js用的js代码:

fauxconsole.js

if (Function.prototype.bind && console && typeof console.log == "object") {
	
    ["log", "info", "warn", "error", "assert", "dir", "clear", "profile", "profileEnd"]
    .forEach(function(method) {
        console[method] = this.call(console[method], console);
    },
    Function.prototype.bind);
}

// log() -- The complete, cross-browser (we don't judge!) console.log wrapper for his or her logging pleasure
if (!window.log) {

    window.log = function() {

        log.history = log.history || []; // store logs to an array for reference
        log.history.push(arguments);

        // Modern browsers
        if (typeof console != 'undefined' && typeof console.log == 'function') {
            // Opera 11
            if (window.opera) {
                var i = 0;
                while (i < arguments.length) {
                    console.log("Item " + (i + 1) + ": " + arguments[i]);
                    i++;
                }
            }
            // All other modern browsers
            else if ((Array.prototype.slice.call(arguments)).length == 1 && typeof Array.prototype.slice.call(arguments)[0] == 'string') {
                console.log((Array.prototype.slice.call(arguments)).toString());
            }
            else {
                console.log(Array.prototype.slice.call(arguments));
            }
        }
        // IE8
        else if (!Function.prototype.bind && typeof console != 'undefined' && typeof console.log == 'object') {
            Function.prototype.call.call(console.log, console, Array.prototype.slice.call(arguments));
        }
        // IE7 and lower, and other old browsers
        else {
            // Inject Firebug lite
            if (!document.getElementById('firebug-lite')) {

                // Include the script
                var script = document.createElement('script');

                script.type = "text/javascript";

                script.id = 'firebug-lite';

                // If you run the script locally, point to /path/to/firebug-lite/build/firebug-lite.js
                script.src = 'https://getfirebug.com/firebug-lite.js';

                // If you want to expand the console window by default, uncomment this line
                //document.getElementsByTagName('HTML')[0].setAttribute('debug','true');
                document.getElementsByTagName('HEAD')[0].appendChild(script);

                setTimeout(function() {
                    log(Array.prototype.slice.call(arguments));
                },
                2000);
            }
            else {
                // FBL was included but it hasn't finished loading yet, so try again momentarily
                setTimeout(function() {
                    log(Array.prototype.slice.call(arguments));
                },
                500);
            }
        }
    }
}

使用方法:console.log("hello world");

在iE中按F12键进入控制台就能查看结果了。


http://q.cnblogs.com/q/33770/


注:console.log在firefox下调试没有问题,但在ie下报“console 未定义”,是因为console 只支持在firefox,不支持ie。




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值