Amsive

PUBLISHED: Feb 17, 2012 1 min read

Using console.log in Later Versions of Magento Under Chrome

Tom DiDomenico

Tom DiDomenico

Senior Vice President, Digital Strategy & Technology

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