11

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

Pekka
  • 418,526
  • 129
  • 929
  • 1,058
AnApprentice
  • 97,752
  • 174
  • 583
  • 971

6 Answers6

30

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

arnorhs
  • 10,218
  • 2
  • 31
  • 38
  • 1
    You can use `console.log()`, just check if `window.console` exists before calling any console functions. E.g. `if (window.console) console.log('Hello World');` – Michal Feb 10 '11 at 22:18
  • Thanks @generalhenry, I didn't know Safari also had it. That's helpful. – arnorhs Feb 10 '11 at 22:27
  • Firefox 4 will also have the console object built in. – sdwilsh Feb 12 '11 at 15:39
4

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

BaggersIO
  • 828
  • 7
  • 10
2

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');
}
Terri Ann
  • 452
  • 3
  • 20
1

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.

bobber205
  • 11,636
  • 25
  • 71
  • 97
  • A [null object](http://en.wikipedia.org/wiki/Null_object) would silently swallow the logging, which would work fine... –  Feb 10 '11 at 22:17
  • 1
    @delnan: I think what bobber means is that `console` has the value `null` (which is not true, it is `undefined`). But in both cases the code will break. – Felix Kling Feb 10 '11 at 22:35
  • @Felix: I know what he propably meant, I was just being pedantic. –  Feb 11 '11 at 10:50
  • @delnan. Shallow and pedantic. ;) – bobber205 Feb 12 '11 at 00:00
1

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

generalhenry
  • 16,677
  • 4
  • 43
  • 59
  • the problem with this function is that it won't accept multiple arguments and parameters the same way console.log() does and also if(console) is not really a good check for if an object is defined or not in javascript. – arnorhs Feb 10 '11 at 22:29
  • the code snipit wasn't intended to be functional, just to get the idea across. I'd post a real example but you beat me too it :p – generalhenry Feb 10 '11 at 22:33
0

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

if ('console' in window) {}
John
  • 10,154
  • 9
  • 79
  • 143