Question

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!

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top