Blog

Using console.log in Later Versions of Magento Under Chrome

Later versions of Magento have added javascript code to /js/varien/js.js to “stub out” the console if it’s not available. This is no doubt to prevent a javascript crash if errant console.log statements get left in throughout the project. The problem is, it prevents console.log from working in Chrome – which I increasingly use just because it’s so much faster.

But there’s a fix (thanks to AlexB in this post http://stackoverflow.com/questions/8080610/javascript-console-log-in-magento for this fix):

Find this block of code in /js/varien/js.js:

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

And change it to:

var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;</HL>
if (!("console" in window) || !("firebug" in console) && !is_chrome)
{
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() {}
}

Leave a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>