'console' is undefined error for Internet Explorer

前端 未结 21 1397
-上瘾入骨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条回答
  •  -上瘾入骨i
    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() {};
    }
    

提交回复
热议问题