繁体   English   中英

为什么Firefox 3会破坏console.log

[英]Why is Firefox 3 breaking with console.log

我有以下内容:

console.log (a.time_ago() + ' ' + b.time_ago());

这在FireFox 3中有所突破,这意味着当FF在JS中击中该行时,它就不会再进一步​​了。 奇怪的是,如果我打开Firebug它不会破坏并继续正常。 一些firebug如何防止这个问题?

我对这个感到困惑。 关于为什么console.log会破坏firefox 3的任何想法,但是如果firebug是打开的话不是吗?

谢谢

这不仅仅是Firefox。 您的代码将停止在每个浏览器中工作(除了Chrome和Safari之外(在某些情况下),因为它们内置了console.log()及其开发人员工具。)

这是因为当你没有打开firebug时,没有定义对象“console”。 您应该注意不要在代码中保留console.log()函数,否则它将在每个浏览器中中断。


我想补充说我有时使用过这个函数:

function log () {
    if (typeof console == 'undefined') {
        return;
    }
    console.log.apply(console, arguments);
}

然后你可以简单地打电话:

log(somevar, anothervar);

它将以与console.log相同的方式工作,但如果未加载firebug则不会失败(并且输入的时间更短:P)

干杯

如果该firebug已关闭,我将覆盖控制台对象。 所以,你可以实现回退功能......

console = console || { log : function() {
// place your logging code here, if firebug is closed
}, debug : function() {
// place your debug code here, if firebug is closed
} /*, [ and so on .. ] */ };

问候,

Dyvor

在FireFox中,如果在调用它时控制台未打开,则会引发JavaScript错误。

我将所有的console.log包装在一个包装器中以检查控制台 - 你可以在控制台调用周围进行检查,或者使用不同的名称来控制别名。

别名

/* konsole is a safe wrapper for the Firebug console. */
var konsole = {
  log: function(args){},
  dir: function(args){},
  debug: function(args){},
  info: function(args){},
  warn: function(args){},
  error: function(args){}
};
// Remove below here when in production
if (typeof window.console != 'undefined' && typeof window.console.log == 'function') {
  konsole = window.console;
}
konsole.log('testing debugging');
konsole.error('throw an error message to the console');

检查控制台

if (typeof window.console != 'undefined' && typeof window.console.log == 'function') {
  console.log('testing debugging');
  console.error('throw an error message to the console');
}

我总是进行if (console)检查以确保控制台确实存在。 如果firebug没有打开,那就像你在对一个零对象进行操作,那么为什么它会破坏。

Firefox没有控制台对象。 Firebug增加了一个。

简单的解决方法是将firebug打开以进行开发,并删除用于部署的console.log语句。

你也可以制作一个自定义日志功能

function log (msg)
{
  if(console)
  {
    console.log(msg);
  }
}

仅在控制台存在时才会记录

为了防止Firefox 3.0 可靠地使用以下内容...

if ('console' in window) {}

暂无
暂无

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

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