“控制台"是 Internet Explorer 的未定义错误 [英] 'console' is undefined error for Internet Explorer

查看:34
本文介绍了“控制台"是 Internet Explorer 的未定义错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Firebug 并且有一些声明,例如:

I'm using Firebug and have some statements like:

console.log("...");

在我的页面中.在 IE8(也可能是早期版本)中,我收到脚本错误,提示控制台"未定义.我试着把它放在我的页面顶部:

in my page. In IE8 (probably earlier versions too) I get script errors saying 'console' is undefined. I tried putting this at the top of my page:

<script type="text/javascript">
    if (!console) console = {log: function() {}};
</script>

我仍然收到错误.有什么办法可以消除错误?

still I get the errors. Any way to get rid of the errors?

推荐答案

尝试

if (!window.console) console = ...

无法直接引用未定义的变量.然而,所有的全局变量都是全局上下文的同名属性(window 在浏览器的情况下),访问未定义的属性是可以的.

An undefined variable cannot be referred directly. However, all global variables are attributes of the same name of the global context (window in case of browsers), and accessing an undefined attribute is fine.

或者使用 if (typeof console === 'undefined') console = ... 如果你想避免魔术变量 window,见 @Tim Down 的回答.

Or use if (typeof console === 'undefined') console = ... if you want to avoid the magic variable window, see @Tim Down's answer.

这篇关于“控制台"是 Internet Explorer 的未定义错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆