7

I have encountered this several times, & there is no explanation for it. There was no JS errors on the site. The code is indeed being executed. Replacing them with alert() works just fine.

So today that happened again, so I tried to check the console object in the console object & I found some irregularities as if something has overwritten the console object.

The following screenshot is of the site (site A) where I have this problem:

http://ompldr.org/vZ256Mw/Selection_004.jpg

You see how log, warn & info are empty functions? and the console object is different from how it is on a site (site B) where console works fine (screenshot follows)?

http://ompldr.org/vZ256Mg/Selection_003.jpg

Now the code running on both the sites is exactly same. Site B is my local install and Site A is staging and I don't have anything in the code which overwrites the console object, so what could be an explanation for this behavior?


As per nfroidure's suggestion, I added that code in my head section before anything else and I got this: uncaught error! (anonymous function) (anonymous function), second of which points to a code in another library file which has the following in it:

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 is responsible because I think the if check is faulty. "console" in window will be true & "firebug" in window will be false for me in Chrome resulting in replacing the console methods. It should be && over there, no?

Update: After changing the operator to && it started to work on Site A, but I don't understand why it only gets triggered on one and not on another.

Ashfame
  • 1,731
  • 1
  • 25
  • 42
  • What code do you have included? Might some third-party code have nulled the `console`? – Bergi Dec 12 '12 at 16:44
  • Is this a bug in the browser? Because you are saying it happens only sometimes. – LPD Dec 12 '12 at 16:46
  • @Bergi A lot, but when I do a grep 'console' on the project, jQuery validation plugin shows up but even that is just checking for the object before using it. – Ashfame Dec 12 '12 at 16:47
  • Or possibly a browser extension is fiddling with `console`? Try disabling all your extensions. – apsillers Dec 12 '12 at 16:47
  • @LPD It has happened to me over a span of 2-3 years – Ashfame Dec 12 '12 at 16:47
  • @apsillers Tried in incognito, still the same without extension – Ashfame Dec 12 '12 at 16:51
  • 1
    Possibly try a debugging tactic I describe here: http://stackoverflow.com/questions/13291814/chrome-javascript-debugging-how-to-break-when-a-value-changes/13291928#13291928 – apsillers Dec 12 '12 at 16:52
  • @apsillers Its unlikely but could be the case. I think if it was in the code, that should happen on my local environment too, right? – Ashfame Dec 12 '12 at 16:56
  • Is everything working 100% properly and the same on local as on staging? All JS files getting included and executed exactly the same etc... ? Is it the case that you can open the browser, visit local site and see OK console, then just change the URL to staging and see broken console? – Treborbob Dec 12 '12 at 17:08
  • @Treborbob yep! exactly same. I don't see anything broken in console. I observed the behavior when I tried to use `console.log` – Ashfame Dec 12 '12 at 17:25
  • I updated the question with some progress I had, which is actually responsible for this, can you guys check? – Ashfame Dec 12 '12 at 17:44

2 Answers2

1

Looks like Site A has firebug included since console has methods that look a lot like the ones here: http://getfirebug.com/wiki/index.php/Console_API

Treborbob
  • 1,221
  • 1
  • 8
  • 18
  • Sorry no, the site doesn't have firebug added to it. – Ashfame Dec 12 '12 at 16:51
  • 1
    …or at least [FireBug lite](http://getfirebug.com/firebuglite) called or some browser extension. Looks very much like that, yes. – Bergi Dec 12 '12 at 16:54
1

Try to add this on the top of your document :

<script>
(function(){
    var wc=window.console;
    delete window.console;
    window.__defineGetter__("console", function(){
        return wc;
        });

    window.__defineSetter__("console", function(val){
        throw('error!');  // Look at js stack when throwed
        });
})();
</script>
nfroidure
  • 1,531
  • 12
  • 20