简体   繁体   English

未安装时忽略firebug控制台

[英]ignore firebug console when not installed

I use Firebug's console.log() for debugging my website. 我使用Firebug的console.log()来调试我的网站。 If I try viewing my website in browsers without Firebug then I get a console is not defined error. 如果我尝试在没有Firebug的浏览器中查看我的网站,那么我得到的控制台没有定义错误。 Is there a way to gracefully avoid this error? 有没有办法优雅地避免这个错误?

I found this potential solution , but it seems a bit cumbersome. 我找到了这个潜在的解决方案 ,但似乎有点麻烦。 And ideas? 和想法?

Firebug source code provides a file to do this : Firebug源代码提供了一个文件来执行此操作:

See firebugx.js 请参阅firebugx.js

Do not reinvent the wheel every day :) 不要每天重新发明轮子:)

I always create my cross-browser wrappers for console.log alike functions and it looks like this: 我总是为console.log相似的函数创建我的跨浏览器包装器,它看起来像这样:

function log(a){
try{
  console.log(a);
  }catch(er){
   try{
     window.opera.postError(a);
     }catch(er){
     //no console avaliable. put 
     //alert(a) here or write to a document node or just ignore
     }
  }

}

It can be extended for any browsers. 它可以扩展到任何浏览器。 in IE when in debug I'd recommend putting this jquery code in last catch: 在IE中调试时我建议将这个jquery代码放在最后一个catch中:

$('body').append('<pre>'+JSON.serialize(a)+'</pre>');

You must add JSON.serialize to Your script. 您必须将JSON.serialize添加到您的脚本中。 IE doesn't have it (IE8 might have, I'm not sure) IE没有它(IE8可能有,我不确定)

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. 编辑当firefox存在时,下面的代码实际上不起作用。 That'll teach for posting code without checking just to show off my not so 1337 operator || 这将教导发布代码而不检查只是为了炫耀我的1337运算符|| skillz: 的skillz:

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

The reason for that is that firefox console is actually a getter only property off window. 原因是firefox控制台实际上只是一个关闭窗口的getter 属性 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? 另外, console.log (和console.warnconsole.error )可以在Webkit浏览器上运行,包括移动Safari,非常酷,对吧?

I don't think it gets much better than the workaround you link to. 我不认为它比你链接到的解决方法好多了。 It's of course possible to melt it down to just defining console.log() and leave off rest, but in essence, you won't get around a construct like this. 当然可以将其融合到仅仅定义console.log()并且不用休息,但实质上,你不会绕过像这样的构造。

Only alternative that comes to mind is checking for console.log every time you call it, and that's even more cumbersome. 只有想到的替代方案是每次调用它时都要检查console.log,这更加麻烦。

Honestly, I'd use that. 老实说,我会用它。 It not only covers console.log() , but also every other console method, and in a decently short number of lines. 它不仅涵盖了console.log() ,还涵盖了所有其他console方法,以及相当短的行数。 The fact that it was first used in the Yahoo media player seems to suggest that it works excellently cross-browser, as well. 它最初在雅虎媒体播放器中使用的事实似乎表明它在跨浏览器方面也非常出色。

That bit of code is your best bet, is actually decently elegant, and should work in most every case. 这段代码是你最好的选择,实际上是优雅的,并且应该适用于大多数情况。 As long as you comment above the snippet just what it is for (so as not to confuse future developers), you should be fine. 只要你在片段上面评论它的用途(以免混淆未来的开发者),你应该没问题。

The solution of @OcuS is sure the best, but you can enhance it with mine: Check this way to log to FF Console: Log to Firefox Error Console from JavaScript @OcuS的解决方案确定最好,但您可以使用我的增强功能:检查这种方式登录到FF控制台: 从JavaScript登录到Firefox错误控制台

Then add to the firebugx.js this 3 lines inside IF: 然后在IF中添加到firebugx.js这3行:

window.console['error'] = li
window.console['warn'] = li
window.console['debug'] = li

So you will see the log of every console error , warn and debug even when the Firebug is closed 因此,即使Firebug关闭,您也会看到每个控制台错误的日志, 警告调试

You can use this code to check if console object exists 您可以使用此代码检查控制台对象是否存在

if('console' in window && 'log' in window.console)
{
    // code using console.log here
}

Or this code 或者这个代码

if(typeof window.console != 'undefined'
&& typeof window.console.log != 'undefined')
{
    // code using console.log here
}

Also you can read this post http://alexandershapovalov.com/firebug-console-is-not-defined-60/ Inside the post link how to create jQuery wrapper for console 你也可以阅读这篇文章http://alexandershapovalov.com/firebug-console-is-not-defined-60/在帖子链接里面如何为控制台创建jQuery包装器

My final solution: 我的最终解决方案

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

Off the top of my head: 脱离我的头顶:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM