【发布时间】:2012-03-15 17:36:17
【问题描述】:
我在编写的一些 JavaScript 中使用了console.log(),在 Internet Explorer 中抛出了一个错误:console is not defined(在其他浏览器中运行良好)。
我已将其替换为:
if (console) console.log("...");
如果console 是undefined,我希望条件评估为false。因此,console.log 语句不会被执行,也不会抛出错误。
相反,会抛出一个错误:console is not defined at character 4。
这是一个 IE 错误吗?或者那个“如果”条件真的是非法的吗?这似乎很荒谬,因为如果if (console) 是非法的,那么if (console==undefined) 也应该是非法的。
您应该如何检查undefined 变量?
【问题讨论】:
-
console在 IE 中不存在,除非控制台实际上是打开的。 -
顺便说一句,您可能还有另一个
console对象,因此万无一失的解决方案是检查console.log是否存在并且是一个函数。 -
我真的写了“Ergo”吗???或者这是@Matt McDonald 编辑的?
-
Ergo 是由最后一位编辑“user1385191”添加的 :)
标签: javascript internet-explorer internet-explorer-8 console undefined