'console' is undefined error for Internet Explorer

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

    You can use the below to give an extra degree of insurance that you've got all bases covered. Using typeof first will avoid any undefined errors. Using === will also ensure that the name of the type is actually the string "undefined". Finally, you'll want to add a parameter to the function signature (I chose logMsg arbitrarily) to ensure consistency, since you do pass whatever you want printed to the console to the log function. This also keep you intellisense accurate and avoids any warnings/errors in your JS aware IDE.

    if(!window.console || typeof console === "undefined") {
      var console = { log: function (logMsg) { } };
    }
    

提交回复
热议问题