Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

javascript - Internet Explorer的“控制台”是未定义的错误('console' is undefined error for Internet Explorer)

I'm using Firebug and have some statements like:(我正在使用Firebug,并且有一些类似的语句:)

console.log("..."); in my page.(在我的页面中。) In IE8 (probably earlier versions too) I get script errors saying 'console' is undefined.(在IE8(可能也是早期版本)中,我收到脚本错误,提示“控制台”未定义。) 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?(有什么办法摆脱错误?)   ask by user246114 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try(尝试)

if (!window.console) console = ... 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.(但是,所有全局变量都是与全局上下文名称相同的属性(对于浏览器为window ),并且可以访问未定义的属性。) Or use if (typeof console === 'undefined') console = ... if you want to avoid the magic variable window , see @Tim Down's answer .(或使用if (typeof console === 'undefined') console = ...如果您想避免魔术变量window ,请参阅@Tim Down的答案 。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...