If you want to keep console.log calls in your app event when your app is used on Firefox without the Firebug plug-in or on IE, you can add this line at the very first start of your JS code :
try { console.log('init console... done'); } catch(e) { console = { log: function() {} } }
This code tries to write some text in the Firebug console. If it fails (no Firebug plug in installed or under IE), it instantiate an object called 'console' that has a method called 'log' that does nothing !
3 comments:
thanks a lot for this one it really helped! the problem was driving me nuts! ^_^
Thanks so much for this solution to a rather annoying problem.
I've expanded it a little bit, because I also use asserts a lot to make sure code doesnt break when changes are made.
try { console.assert(1); } catch(e) { console = { log: function() {}, assert: function() {} } }
Also, this means that the check can be performed without logging a message to the console if it is active.
I know this is old, but I'm researching solutions for this problem in the last week, and Clint's version is by far the best I've seen. Thanks!
I had elaborated it to allow more console APIs, so here goes:
try {
console.assert(1);
}
catch(e) {
console = {
log: function() {},
debug: function() {},
info: function() {},
warn: function() {},
error: function() {},
assert: function() {},
dir: function() {},
dirxml: function() {},
trace: function() {},
group: function() {},
groupCollapsed: function() {},
groupEnd: function() {},
time: function() {},
timeEnd: function() {},
profile: function() {},
profileEnd: function() {},
count: function() {},
exception: function() {},
table: function() {}
}
}
Post a Comment