检测浏览器是否开启firebug以及如何避免调试信息带来的脚本错误

来源:互联网 发布:淘宝网数据包下载 编辑:程序博客网 时间:2024/05/16 09:36

今天发现使用Gmail的时候开启firebug,会给出提示“在已知情况下,除非正确配置 Firebug,否则它会使 Gmail 运行缓慢。解决此问题 隐藏”,感叹Gmail真是事无巨细,面面都考虑到了。

于是想了解Gmail是如何检测用户是否开启firebug的,初步判断应该是检测是否存在window.console对象,查了下资料,果然如此:

?
1
2
3
if(window.console && window.console.firebug){
//开启了firebug
}

延伸一下,有时候写代码的时候为了调试方便,在代码里面写了不少调试的语句,但是万一firebug没开程序就走不下去了,于是便有了下面这段:

?
1
2
3
4
5
6
7
if(!window.console){
    varemptyfun = function(){};
    window.console = {};
    "log debug info warn exception assert dir dirxml trace group groupEnd groupCollapsed time timeEnd profile profileEnd count clear table error notifyFirebug".replace(/\w+\b/g,function(o){
        window.console[o] = emptyfun;
    });
}

原理很简单,就是创建空的调试方法;

原创粉丝点击