为什么 Firefox 3 无法使用 console.log

发布于 2024-10-17 01:19:50 字数 277 浏览 3 评论 0原文

我有以下内容:

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

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

我对此很困惑。有没有想过为什么 console.log 会破坏 Firefox 3,但如果 Firebug 打开则不会?

谢谢

I have the following:

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

This is breaking in FireFox 3, meaning when FF hits that line in the JS, it goes no further. Strangely if I have Firebug open it doesn't break and continues as normal. Some how firebug prevents this issue?

I'm puzzled on this one. Any thoughts as to why console.log would break firefox 3, but not if firebug is open?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

暖风昔人 2024-10-24 01:19:50

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

这是因为当您没有打开 firebug 时,对象“控制台”未定义。您应该注意不要在代码中留下 console.log() 函数,否则它会在每个浏览器中崩溃。


我想补充一点,我有时会使用这个函数:

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

然后你可以简单地调用:

log(somevar, anothervar);

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

干杯

This is not just Firefox. Your code will stop working in every browser (except Chrome and safari (in some instances) because they have console.log() built in along with their developer tools.)

It is because when you don't have firebug open, the object "console" is not defined. You should take care never too leave console.log() functions in your code, or it will break in every browser.


I'd like to add that I have sometimes used this function:

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

Then you can simply call:

log(somevar, anothervar);

and it will work the same way as console.log, but will not fail if firebug is not loaded (and is shorter to type :P)

Cheers

安人多梦 2024-10-24 01:19:50

如果该萤火虫关闭,我会覆盖控制台对象。所以,你可以实现后备功能......

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

In case, that firebug is closed, I overwrite the console object. So, you can implement fallback functions ...

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 .. ] */ };

Greetings,

Dyvor

病毒体 2024-10-24 01:19:50

在 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');
}

In FireFox if the console isn't open when you call to it a JavaScript error is thrown.

I wrap all my console.log's in a wrapper to check for console - you can either wrap the check around the console call or use a different name to alias console.

Aliasing

/* 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');

Check for console

if (typeof window.console != 'undefined' && typeof window.console.log == 'function') {
  console.log('testing debugging');
  console.error('throw an error message to the console');
}
冬天旳寂寞 2024-10-24 01:19:50

我总是进行 if (console) 检查以确保控制台确实存在。如果 firebug 没有打开,就好像您正在对一个 null 对象进行操作,这就是它崩溃的原因。

I always do a if (console) check to make sure the console actually exists. If firebug isn't open it's like you're acting upon a null object, thus why it breaks.

玩心态 2024-10-24 01:19:50

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

简单的修复方法是打开 firebug 进行开发,并删除 console.log 语句进行部署。

您还可以创建一个自定义日志函数,

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

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

Firefox doesn't have a console object. Firebug adds one.

The simply fix is to have firebug open for development and remove console.log statements for deployment.

you can also make a custom log function like

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

that will log only if console exists

浅听莫相离 2024-10-24 01:19:50

为了让 Firefox 3.0 可靠地避免抱怨,请使用以下命令...

if ('console' in window) {}

To keep Firefox 3.0 from complaining reliably use the following...

if ('console' in window) {}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文