1

I checked my index.php and removed all the console.log() but when I go to IE7 I am still getting console errors. The funny thing is if I check the in IE9 and then change the browser mode to IE7 it works fine, however if test the site straight into an IE7 browser it gives a console error message.

Any idea on how to stop this please?

Aessandro
  • 5,517
  • 21
  • 66
  • 139
  • 4
    It's really hard to say how to get rid of an unspecified error generated by unspecified code. – Quentin Oct 10 '12 at 15:21
  • 2
    If you mean you are getting *console is undefined* errors in IE7 where there is no console; http://stackoverflow.com/questions/3326650/console-is-undefined-error-for-internet-explorer – Alex K. Oct 10 '12 at 15:22
  • 1
    Are you including specific js files for older browsers (`<! IF[IE 7]>` and the likes), and have you tried clearing your browser cache? (Ctrl+F5 a couple of times usually does the trick) – Elias Van Ootegem Oct 10 '12 at 15:32

1 Answers1

2

You probably missed a console.log call somewhere. The reason why it works in IE9 switching to IE7 is that the console object is created as soon as you open the console to switch the version. Thus no error will be thrown in the IE7 emulation mode of IE9.

The better solution is to include the following in your js-code beforehand:

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

Now all your console.log calls won't throw an error but will be silently ignored.

I do the following when developing:

var logging = 1;
if (!window.console){console = {log:function(){}};}
function _log(a) { if(logging) { console.log(a); } }

and keep on using _log("foo") for the entire code. Now you can switch logging with a simple logging = 0 and are safe against the missing console object.

Christoph
  • 50,121
  • 21
  • 99
  • 128
  • +1, yes. I usually use consolelog() instead of console.log, which checks for console and console.log being defined. That way if I pop up the console during, messages will instantly log. – Ed Bayiates Oct 10 '12 at 15:45