'console' is undefined error for Internet Explorer

前端 未结 21 1385
-上瘾入骨i
-上瘾入骨i 2020-11-22 04:49

I\'m using Firebug and have some statements like:

console.log(\"...\");

in my page. In IE8 (probably earlier versions too) I get script err

相关标签:
21条回答
  • 2020-11-22 05:25

    In IE9, if console is not opened, this code:

    alert(typeof console);
    

    will show "object", but this code

    alert(typeof console.log);
    

    will throw TypeError exception, but not return undefined value;

    So, guaranteed version of code will look similar to this:

    try {
        if (window.console && window.console.log) {
            my_console_log = window.console.log;
        }
    } catch (e) {
        my_console_log = function() {};
    }
    
    0 讨论(0)
  • 2020-11-22 05:25

    You can use console.log(...) directly in Firefox but not in IEs. In IEs you have to use window.console.

    0 讨论(0)
  • 2020-11-22 05:30

    Sometimes console will work in IE8/9 but fail at other times. This erratic behaviour depends on whether you have developer tools open and is described in stackoverflow question Does IE9 support console.log, and is it a real function?

    0 讨论(0)
提交回复
热议问题