ignore firebug console when not installed

2019-03-10 09:45发布

I use Firebug's console.log() for debugging my website. If I try viewing my website in browsers without Firebug then I get a console is not defined error. Is there a way to gracefully avoid this error?

I found this potential solution, but it seems a bit cumbersome. And ideas?

9条回答
混吃等死
2楼-- · 2019-03-10 10:16

Firebug source code provides a file to do this :

See firebugx.js

Do not reinvent the wheel every day :)

查看更多
爷的心禁止访问
3楼-- · 2019-03-10 10:21

The linked solution is basically a variant(with a few extra functions) of this:

EDIT The below code doesn't actually work when firefox is present. That'll teach for posting code without checking just to show off my not so 1337 operator || skillz:

window.console = window.console || {};
console.log = function(){};

The reason for that is that firefox console is actually a getter only property off window. Hence we can't set it. Instead, something like this needs to be used:

if (!window.console) {
  window.console = {};
  window.console.log = function(){};
}

Also, console.log (and console.warn, console.error) will work on Webkit browsers, including mobile Safari, pretty cool, huh?

查看更多
时光不老,我们不散
4楼-- · 2019-03-10 10:21

My final solution:

if(!("console" in window)) 
    window.console = {log: function() {}};
查看更多
登录 后发表回答