28

Which solution do you recommend, the second is simpler ( less code ), but there are drawbacks on using it ?

First: (Set a global debug flag)

// the first line of code
var debug = true;
try {
    console.log
} catch(e) {
    if(e) {
        debug=false;
    }
};
// Then later in the code
if(debug) {
    console.log(something);
}

Second: override console.log

try {
    console.log
} catch(e) {
    if (e) {
        console.log = function() {}
    }
};
// And all you need to do in the code is
console.log(something);
Sachin Jain
  • 21,353
  • 33
  • 103
  • 168
Radu Maris
  • 5,648
  • 4
  • 39
  • 54

7 Answers7

56

Neither, but a variation of the second. Lose the try...catch and check for existence of the console object properly:

if (typeof console == "undefined") {
    window.console = {
        log: function () {}
    };
}

console.log("whatever");
Andy E
  • 338,112
  • 86
  • 474
  • 445
  • 1
    Presumably you either want to do `this.console = ...` or `var console = ...`? As you have it at the moment, you'd get an error in ECMAScript 5 strict mode. – Tim Down Sep 22 '10 at 09:48
  • 1
    @Tim: thanks, it was an oversight. I think `window.console` would be best for portability. – Andy E Sep 22 '10 at 09:56
  • 1
    Portability in the sense of being able to move this code into a function, rather than portability between environments? – Tim Down Sep 22 '10 at 10:14
  • 1
    Should you test typeof window.console since you're setting window.console later? Or should window. be removed? – jcolebrand Oct 25 '11 at 22:54
  • Definitely check out the solution by Suresh below - he is also defining other console methods that may not be defined. See https://developer.mozilla.org/en-US/docs/Web/API/Console for a full list of all console methods available and what the browser support is like. – Tim Holt Sep 24 '15 at 16:42
6

Or, in coffeescript:

window.console ?=
    log:-> #patch so console.log() never causes error even in IE.
Jameson Quinn
  • 1,060
  • 15
  • 21
3

EDIT: Andy's answer is way more elegant than the quick hack I've posted below.

I generally use this approach...

// prevent console errors on browsers without firebug
if (!window.console) {
    window.console = {};
    window.console.log = function(){};
}
Community
  • 1
  • 1
Frankie
  • 24,627
  • 10
  • 79
  • 121
  • 1
    I like your version Frankie, but I'm not sure that it works safely across all browsers. I remember using this and still getting occasional issues with one of the IE versions, perhaps because `console` object is only defined when the console window is open in IE9. I think I had to do an 'undefined' check like Andy E suggested. – Simon East Sep 23 '11 at 02:58
  • @Simon this comes late as a comment but only saw it today. Andy's solution is way more elegant than this quick hack I've posted. You should use that instead. – Frankie Jun 15 '13 at 16:34
1

I've faced a similar bug in my past, and I overcame it with the code below:

if(!window.console) {
    var console = {
        log : function(){},
        warn : function(){},
        error : function(){},
        time : function(){},
        timeEnd : function(){}
    }
}
Community
  • 1
  • 1
Suresh
  • 923
  • 1
  • 10
  • 21
  • How is this different than Frankie or Andy_E answers ? – Radu Maris Jun 17 '14 at 13:23
  • The answer by Suresh is better in that he is also defining other methods that will not be defined along with log. Check out https://developer.mozilla.org/en-US/docs/Web/API/Console for a full list of all functions that may (or may not) be defined. – Tim Holt Sep 24 '15 at 16:40
  • This won't work if it's called from within a function. Instead of `var console` it should be `window.console` or just `console` – Glenn Lawrence Nov 11 '15 at 10:54
0

I came across this post, which is similar to the other answers:

http://jennyandlih.com/resolved-logging-firebug-console-breaks-ie

David
  • 3,223
  • 3
  • 29
  • 41
0

The following will achieve what you are looking for:

window.console && console.log('foo');
Mike Kormendy
  • 3,389
  • 2
  • 24
  • 21
0
window.console = window.console || {};
window.console.log = window.console.log || function() {};
Sean
  • 779
  • 6
  • 18