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);

7 Answers 7

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");
Sign up to request clarification or add additional context in comments.

5 Comments

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: thanks, it was an oversight. I think window.console would be best for portability.
Portability in the sense of being able to move this code into a function, rather than portability between environments?
Should you test typeof window.console since you're setting window.console later? Or should window. be removed?
Definitely check out the solution by Suresh below - he is also defining other console methods that may not be defined. See 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.
6

Or, in coffeescript:

window.console ?=
    log:-> #patch so console.log() never causes error even in IE.

Comments

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(){};
}

2 Comments

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 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.
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(){}
    }
}

3 Comments

How is this different than Frankie or Andy_E answers ?
The answer by Suresh is better in that he is also defining other methods that will not be defined along with log. Check out developer.mozilla.org/en-US/docs/Web/API/Console for a full list of all functions that may (or may not) be defined.
This won't work if it's called from within a function. Instead of var console it should be window.console or just console
0

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

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

Comments

0

The following will achieve what you are looking for:

window.console && console.log('foo');

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.