What is the best approach to prevent errors when console.log calls remain in JavaScript and it runs in browsers without a console or with the console turned off. Is there a way that it can be automatically redefined to become a javascript alert, for example?
if(!window.console) console = {log: function(s) {alert(s);}};
You can, of course, add more features that the console usually has.
, console, typeof, window.console, , , , ReferenceError.
console
typeof
window.console
:
if (typeof console == "undefined") { window.console = { log: function () { // do nothing } }; console.warn = console.debug = console.log; }
Here is what I use: -
if(typeof(console) != "undefined")