0

This question is a follow-up to this discussion: Why does JavaScript only work after opening developer tools in IE once?

Please see this fiddle that shows that the console.log is no longer an issue in MSIE9: http://jsfiddle.net/xwsYY/11/

$('button.something').click(function () {
    $('div.container').html('<p>something</p>');
    console.log('microsoft strikes again!');
});

I'm sorry, but I can't show you my actual code that is having this problem, but it checks out in all other browsers but will only run in MSIE9 after the console has been opened and the page has been refreshed. Then it's fine. No comment about how I feel about this.

If anyone else has experienced this problem, please help me out!

Community
  • 1
  • 1
  • possible duplicate of [Does IE9 support console.log, and is it a real function?](http://stackoverflow.com/questions/5472938/does-ie9-support-console-log-and-is-it-a-real-function) – adeneo Jun 12 '13 at 21:58
  • What I'm saying is that the console.log problem does not seem to exist any more. http://jsfiddle.net/xwsYY/16/ – Richard_Peterson Jun 12 '13 at 22:27
  • 1
    It doesn't exist in MSIE10, indeed. But nothing changed in MSIE9. – nrodic Jun 12 '13 at 22:31
  • Well two years is an awfully short time to expect Microsoft to fix something, I suppose. Thanks for looking at this! – Richard_Peterson Jun 12 '13 at 22:46

2 Answers2

1

Unfortunately, you're wrong about console "no longer being an issue in IE9". It is an issue, and it is exactly as described in the question you've linked to.

Yes, the code you've quoted and jsFiddled will run in IE9 - even with the console window closed - but that is only because the call to console is the last thing in the function. The function runs perfectly fine until it hits the console call, and then it fails silently. So it looks like it's running perfectly.

If you put the console call before the other line of code, then you'll find that it doesn't work.... unless you've opened the console first, of course, which takes us back to the answer to the previous question.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • I mistakenly posted my reply above; here it is again: Please try this code. Seems to me that it works: var i = 0; console.log('microsoft strikes again!'); $('button.something').click(function () { $('div.container').html('

    something ' + i + '

    '); console.log('microsoft strikes again!' + i); i++; }); – Richard_Peterson
    – Richard_Peterson Jun 12 '13 at 22:15
1

I had similar question and best answer is that MSIE doesn't have console object until F12 tools have been open. So javascript throws error. Once you open developer tools, console exists and console.log() doesn't throw.

I use this code to maintain compatibility during development:

try {
    if (typeof console == "undefined") {
        this.console = {
            log: function () {}  //warn, error, ...
        };
    }
} catch (e) {
    this.console = {
        log: function () {}  //warn, error, ...
    };
}

However, it is recommended to remove all console references from production code.

EDIT: Changed window into this to access global scope as it pleases MSIE more.

nrodic
  • 3,026
  • 3
  • 33
  • 38
  • Yes, I've looked at the other stuff, the "already answered" stuff. I'm saying that console.log() does not seem to be the problem. I've tried the thing where you substitute your own console.log object if one doesn't exist. No change. – Richard_Peterson Jun 12 '13 at 22:23
  • @Richard_Peterson Have a look at [updated jsfiddle](http://jsfiddle.net/xwsYY/15/). Restart browser, don't open developer tools and load this fiddle. I would say it works. – nrodic Jun 12 '13 at 22:27
  • 1
    I have to agree with you that your code works. Thanks very much for clarifying it. I will have to have at my project again and hope that I can find where I went wrong. But you know what? Microsoft should fix this stuff. What is supposed to be in these "updates" that they keep bugging me with?! This problem is over 2 years old now! – Richard_Peterson Jun 12 '13 at 22:41