js override console.log if not defined

32,803

Solution 1

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

Solution 2

Or, in coffeescript:

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

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

Solution 4

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(){}
    }
}
Share:
32,803

Related videos on Youtube

Radu Maris
Author by

Radu Maris

Happily married for more than 12 years, father of an 8y energetic boy. Currently remote working as a System Designer, for a very nice company in southern Germany. I love software development (create things), I think that with software you could build everything, it's just a metter of how hard it is to build it, and I don't like to settle for anything less than high quality software. For me, high quality means: great user experience, fast and most important reliable. I need technical challenges in my day to day life, without them I would be really bored. I'm addicted to science and technology (software, internet, physics, mechanics, astronomy, you name it).

Updated on July 09, 2022

Comments

  • Radu Maris
    Radu Maris almost 2 years

    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);
    
  • Tim Down
    Tim Down over 13 years
    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.
  • Andy E
    Andy E over 13 years
    @Tim: thanks, it was an oversight. I think window.console would be best for portability.
  • Tim Down
    Tim Down over 13 years
    Portability in the sense of being able to move this code into a function, rather than portability between environments?
  • Simon East
    Simon East over 12 years
    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.
  • jcolebrand
    jcolebrand over 12 years
    Should you test typeof window.console since you're setting window.console later? Or should window. be removed?
  • Frankie
    Frankie almost 11 years
    @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.
  • Radu Maris
    Radu Maris almost 10 years
    How is this different than Frankie or Andy_E answers ?
  • Tim Holt
    Tim Holt over 8 years
    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.
  • Tim Holt
    Tim Holt over 8 years
    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.
  • Glenn Lawrence
    Glenn Lawrence over 8 years
    This won't work if it's called from within a function. Instead of var console it should be window.console or just console