5

I have a JS script that works fine in all browsers. But for everybody's surprise, in I.E. it does not work at the first try.

If, after I load my page, I press F12 (open the i.e. debugger) and refresh my page, it works fine! Just like the others browsers! But for this work, i have to press F12.

Does i.e.'s debugger do something when we open it? I cant find a solution!

Thanks in advance.

Fernando Ferrari
  • 485
  • 6
  • 19
  • 1
    possible duplicate of ['console' is undefined error for internet explorer](http://stackoverflow.com/questions/3326650/console-is-undefined-error-for-internet-explorer), [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) – kapa Jul 05 '12 at 14:15

3 Answers3

12

When you don't have the debugger open, IE considers there to be no such thing as console.log, and gives you errors for calling an undefined function. When you hit F12, then you get the console, and so console.log is no longer undefined.

You can workaround by putting this at the top of your code:

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

Rather than editing out console.log from your code, this will merely make the browser do nothing if the console doesn't exist, by defining them as a 'do nothing' function if they are undefined.

If you're looking to shrink down your js file size (particularly important for mobile usage), you will eventually want to remove verbose logging on your release version.

sylverfyre
  • 2,440
  • 3
  • 15
  • 15
  • awesome! thank you so much. it worked. I have removed my console logs, because i do not really need them. – Fernando Ferrari Jul 05 '12 at 14:20
  • This particular problem also caught me out before. You can add something like `if(typeof(console) == 'undefined') console = {log : function (text) {} };` to your code as well to keep your console.log statements. – Wernsey Jul 05 '12 at 14:23
  • iWerner, that's not a bad idea if you want to be able to access the logs even when you're not running a console. I did things this way because I figured if I don't have a console open, I probably wouldn't need the logging to do anything at all, so I just left it as a "do nothing" function. – sylverfyre Jul 05 '12 at 18:11
  • Wish I'd seen this a week ago. – Matt Parkins Apr 16 '13 at 15:25
2

Do you have something like console.log() in your script? This might explain, since there is no console until you press F12

2

Extended version from previous post

if (!('console' in window)) {
    var stub = function() { ; };
    window.console = {
        log : stub,
        info : stub,
        warn : stub,
        error : stub,
        assert : stub
    };
}

I'm posting this new one that install stub only if needed

/**
 * On IE console is not set if not opened and debug doesn't exists
 */
(function() {
    if (!('console' in window)) { window.console = {}; }
    var kind = ['log', 'info', 'warn', 'error', 'assert', 'debug'];
    var stub = function() { ; };
    for (var i = 0; i < kind.length; i++) {
        if (kind[i] in window.console) { continue; }
        window.console[kind[i]] = stub;
    }
})();
showi
  • 456
  • 1
  • 4
  • 8