'console' is undefined - javascript error in logs

Error from log
Discourse 1.2.0.beta4 - https://github.com/discourse/discourse version 3cea85e09a5a86d7a965fb2ffd8b132659b79db0

##Info

'console' is undefined
Url: http://example.com/assets/vendor-0dbc6e71b0ee5428574e3562afe91ef1.js
Line: 16
Window Location: http://example.com/

##Env

HTTP_HOST: example.com
REQUEST_URI: /logs/report_js_error
REQUEST_METHOD: POST
HTTP_USER_AGENT: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0)
HTTP_ACCEPT: */*
HTTP_REFERER: http://example.com/


params:

      message: 'console' is undefined
    Url: http://example.com/assets/vendor-0dbc6e71b0ee5428574e3562afe91ef1.js
    L
      url: http://example.com/assets/vendor-0dbc6e71b0ee5428574e3562afe91ef1.js
      line: 16
      window_location: http://example.com/

IE 9 on Windows Vista

Older IE’s don’t have console and you will see this error.

Best way to handle this is to not use console and use another lib to manage writing, or use a shim.

1 Like

We only have to support IE9 and barely. So we need to figure out where that’s being called from @eviltrout.

On reviewing your suppression for logster…
https://github.com/discourse/discourse/commit/a2e77d8bf49c41c3a56515afe7dc118d8f88725f

I think the best thing to do would be add the following simple code, the reason for this is that the code after the failed console.log will not be executed causing unexpected user experience that might just work fine otherwise.

if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function () {};

In a year or so we will probably drop IE9 support altogether so I am not sure it is worth it.

IE 9 has been our minimum browser since launch in Feb 2013 and it has been almost two years now…

Also checking browser stats today, IE9 is quite rare. IE8 is much more globally common mainly because Windows XP users cannot upgrade to any higher version of IE. And we do not even pretend to work on IE8 of course, we sort of work on IE9 in an “absolute bare bones minimum” kind of way.

I will try to track down where that console.log is coming from though.

Sadly 12.75% of traffic for one of the communities I am involved with came from IE8 & 9 in the past 30 days - 11.6% IE9.

Well at least the IE8 number is low! I agree the basics should continue to work on ie9 for at least the next year.

Actually IE8 doesn’t have the console.log issue - it’s only IE9 - IE9 requires the debug console open for console to exist.

The only console.log I could find that wasn’t buried in external dependencies or in testing was here in I8n.

https://github.com/discourse/discourse/commit/3d0e59942cfc786e362924d2e0d3d617cc18599a

Hopefully that’s it.

That’s there on purpose for verbose localization which is default off…

It’s in vendor.js just grep the unminified

Problem is there are ~150 instances of console.log, probably ~100 of those are in external dependencies and I can’t tell which ones matter… looks like some of the dependencies set up custom handling for the console, e.g.

// Console
var console = window.console = {};
console.log = console.info = console.warn = console.error = function(){};

and

if ( typeof console !== "undefined" && typeof console.log !== "undefined" )
  console.log.apply( null, args );

Maybe it’s better to look at it this way: is anything unusual (other than pushstate, which doesn’t exist) broken in IE9?

Can you answer that question @DeanMarkTaylor ? That might be more useful to us than regurgitating a bunch of javascript error logs submitted by the client… I did download the IE9 vm which is 3.5 gb, but it’s hard to make time on MLK day with 3 kids at home, etc.

I can’t talk for Discourse specifically, but this will affect IE8 and 9 - basically, console gets created the first time a user opens the developer console (ever, in the whole life of that browser) and exists for ever more.

Speaking from experience it is an extremely annoying problem to track down, and you will more than likely miss some other features that aren’t working.

I have found that building a fake console (as in @codinghorror post) is the easiest way to deal with this.

J

Created a new topic to cover IE9 test results:

Right now our Javascript linting is designed to raise errors on console.log and not push to tests-passed. however they can still sneak in sometimes in files that are not linted properly.

Also we still sneak in logs using the {{log}} helper in Ember, but I checked their implementation and it seems like it should work in IE9.

Filling it with an empty function like @DeanMarkTaylor suggested is a pretty good solution. I could make an initializer to do that and call it “ie9-hax” or something to make it easy to find later on.

3 Likes

I believe this topic can be closed thanks to your commit @eviltrout

https://github.com/discourse/discourse/commit/df8880a71a20e2d845934b2ab6e8515b3808eeb2

Unless you think there is something else to do with linting?