'console' is undefined error for Internet Explorer

前端 未结 21 1343
-上瘾入骨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:22

    In my scripts, I either use the shorthand:

    window.console && console.log(...) // only log if the function exists
    

    or, if it's not possible or feasible to edit every console.log line, I create a fake console:

    // check to see if console exists. If not, create an empty object for it,
    // then create and empty logging function which does nothing. 
    //
    // REMEMBER: put this before any other console.log calls
    !window.console && (window.console = {} && window.console.log = function () {});
    

提交回复
热议问题