js override console.log if not defined
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(){}
}
}
Related videos on Youtube
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, 2022Comments
-
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 over 13 yearsPresumably you either want to do
this.console = ...
orvar console = ...
? As you have it at the moment, you'd get an error in ECMAScript 5 strict mode. -
Andy E over 13 years@Tim: thanks, it was an oversight. I think
window.console
would be best for portability. -
Tim Down over 13 yearsPortability in the sense of being able to move this code into a function, rather than portability between environments?
-
Simon East over 12 yearsI 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 over 12 yearsShould you test typeof window.console since you're setting window.console later? Or should window. be removed?
-
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 almost 10 yearsHow is this different than Frankie or Andy_E answers ?
-
Tim Holt over 8 yearsThe 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 over 8 yearsDefinitely 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 over 8 yearsThis won't work if it's called from within a function. Instead of
var console
it should bewindow.console
or justconsole