Javascript Console.log Not working in Chrome, or Firefox

10,444

Solution 1

I just came across this problem after a Firefox update and managed to fix it. Here's the code that caused the problem:

/* IE fix that allows me to still log elsewhere */
if (typeof(console)=="undefined") {
    var console = {
        output: null,
        log: function (str) {
            // we can't emulate the console in IE, but we can cache what's output
            // so that IE users can get it via console.output
            if (!this.output) this.output = new Array();
            this.output.push(new String(str));
        }
    };
    window.console = console;
}

In the previous version of FireFox, "var console;" wouldn't get executed. Now it seems to have added some sort of branching/prediction mechanism. Seeing that I may define a variable called console with global scope, it disables window.console.

I fixed this issue by renaming var console; to var cons;

/* IE fix that allows me to still log elsewhere */
if (typeof(console)=="undefined") {
    var cons = {
        output: null,
        log: function (str) {
            // we can't emulate the console in IE, but we can cache what's output
            // so that IE users can get it via console.output
            if (!this.output) this.output = new Array();
            this.output.push(new String(str));
        }
    };
    window.console = cons;
}

I still need to test this to make sure it does what I expect in IE, though. I just need to find a copy of IE without a console (I think 9 or below).

I just wish that Firefox would tell you what line of what script disabled the console - that would be nice.

Solution 2

Turns out the theme developer had added firebug lite to the theme without me knowing. Turning it off fixed the problem.

Solution 3

I had the same issue with a Magento e-commerce site (version 1.6.2.0).

I fixed it commenting the following lines in /js/varien/js.js:637

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

This fix is only (obviously) for Magento sites.

Share:
10,444
BlackHatSamurai
Author by

BlackHatSamurai

I am a mobile application developer, specializing in Android applications. I have experience with a number of different technologies including Java, Ruby, PHP, Objective C, HTML, CSS, Javascript, &amp; C#. I am a life long learner and enjoy the development process. I hope to be able to contribute to the community in a positive light. Cheers!

Updated on June 05, 2022

Comments