console.log() is defined but doesn't log in IE

2019-07-04 00:14发布

问题:

Javascript on a page in Internet Explorer (8 and 9) with Developer Tools open reaches console.log(), which is defined - but nothing appears in the actual log.

Things tried:

  • Double-checking with alert()s that console is defined and that console.log is a function.
  • Checking with alert()s before and after that the code does reach the console.log() line,
  • Checking code for any IE fallbacks like if(ie){console={log:function(){}}}, removing links to libraries and checking code snippets for mentions of console
  • Checking IE's settings and enabling anything relating to debugging
  • Checking that IE isn't broken by loading a 3rd party page with console.log()s (e.g. http://jsbin.com logs "init" and "runner")
  • Swapping console for window.console

So the console is there and active, but no console messages show up in the actual console (on either the Script tab or the Console tab).

What else could stop console.log() from actually logging anything, even when Developer Tools is open and console.log is a defined function?

回答1:

The culprit in this case turned out to be, of all things, firebug lite.

My test dev pages often include this to (ironically) aid certain types of debugging in IE:

<!--[if IE]>
<script type="text/javascript" src="https://getfirebug.com/firebug-lite.js"></script>
<![endif]-->

Since it appears in text editors as a comment, it's easy to accidentally overlook.

Alerting alert(console.log); before and after shows firebug lite changes the console.log function from the native code to this:

function(){return f.apply(c,arguments)}

...which for some reason (at the moment, here) then does nothing.

Without Firebug Lite getting involved, alert(console.log); in IE gives this:

function log() {
[native code]
}

...and does its usual trick of logging if f12 Dev Tools is open and crashing if it isn't.