This has been bugging me for a while, I’ve done some searching and couldn’t come up with the fix. But I found it today and greatly appreciate those that helped me to find this.
The Problem:
I’m working on a Magento site doing some development – some Javascript stuff. As we all know, a simple console.log message can be a very invaluable tool for troubleshooting issue. But it never shows up – not in Chrome, nor Firebug (Firefox)… I didn’t try IE, but who the heck uses that anyway? And it turns out IE doesn’t work for this either according to my sources.
I found some sources saying that you can add code to disable console.log. This Stack Overflow question talks about it. But I couldn’t find any traces to this in my code base, so that wasn’t it…
Then I came across this question on Stack Overflow and it smacked the nail right on the head! Thanks to sg3s for the answer. As he points out, this is something Magento put in place in the following file: magento/js/varien/js.js
1 2 3 4 5 | if(!("console"in window)||!("firebug"in console)){ var names =["log","debug","info","warn","error","assert","dir","dirxml","group","groupEnd","time","timeEnd","count","trace","profile","profileEnd"]; window.console ={};for(var i =0; i < names.length;++i) window.console[names[i]]=function(){} } |
You could of course alter that file and comment out this code, but I’m not a fan of altering the core code and this could cause issues in production. So instead I’m taking his solution and using that as needed.
The Solution:
It’s simple actually – delete the console variable that Magento set, which then allows your code to take over again. So at the beginning of your code, simply add:
1 | delete window['console']; |
Now this is not something that should be used in production, so as you finalize your code and are deleting all of your other console.log lines of code, just make it practice to delete this as well and you are all set.
But why block console.log?
Well for those of you that don’t know, older versions of IE have issues with this as the variable console isn’t defined UNLESS you have your debugger console open – and that didn’t exist until IE8. So the site will throw a Javascript error because you are trying to call a variable, console, that doesn’t exist. That’s the reason that some developers will disable console.log. Great to ensure a quality production site, but something that will drive you bonkers when you’re trying to do development!