10

I have a strange problem in Internet Explorer with JavaScript. In every browser I did the test the JavaScript is enabled, but it seems to run only after I press the F12, running it in debug mode. And what is more confusing, after starting the IE debugger everything is working as suppose to.

Any ideas what it could be?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Constantin
  • 2,288
  • 2
  • 24
  • 31

1 Answers1

25

If you're calling:

console.log('...some text here...');

or any related method of console without having checked if window.console exists, the script will fail silently. Opening the console leads to window.console existing, which allows the script to continue execution.

Add "window.console && " before your calls to console:

window.console && console.log('works');
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • 2
    Or, just write a wrapper method: `function log(s){ window.console && console.log(s); }`, to keep the code readable... – hiobs Sep 29 '11 at 19:28
  • thanks guys :D i will keep in mind that from IE family only IE9 has a console :P – Constantin Sep 29 '11 at 19:34
  • @Constantine, that's not true. Any IE version can have a dynamically added console with firebug lite. – zzzzBov Sep 29 '11 at 19:43
  • this is so good! thanks man.. i mean, if it would not cost me such much lifetime and nerves it would actually funny how many different bugs the fkn IE always has – tmaximini Oct 14 '11 at 14:57
  • actually Firefox did this too not so long ago. I actually thought it still does without Firebug (though it would not fail *silently*) – dualed Jan 23 '13 at 15:53
  • Great info... had no idea about this – ponysmith Oct 07 '13 at 22:21