Common idiom to avoid IE throw: Error: #39;console#39; is undefined(避免 IE 抛出的常见习语:错误:console is undefined)
问题描述
我已经安装了 firebug 并编写了所有这些日志语句.
I've installed firebug and I wrote all these log statements.
我已经在 IE 中测试了我的应用程序,当然我遇到了未定义"错误.
I've tested my app in IE and of course I've got "undefined" error.
避免这种情况的常用习语是什么.
What's the common idiom to avoid this.
我真的不想评论我文件中的所有 console.log 语句,也不想模拟它们.
I don't really feel like commenting all the console.log statements in my file nor to mock them.
嗯,我不知道该怎么办.
Well I'm not sure what to do.
推荐答案
我通常做一个这样的包装函数:
i usually make a wrapper function like so:
function log(obj) {
if (window.console && console.log) console.log(obj);
}
或者你可以在你的脚本文件/元素的开头做这样的事情:
or you could do something like this at the beginning of your script file/element:
if (!window.console) {
window.console = {
log: function(obj){ /* define own logging function here, or leave empty */ }
};
}
这篇关于避免 IE 抛出的常见习语:错误:'console' is undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:避免 IE 抛出的常见习语:错误:'console' is u


- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01