避免IE throw的常见习语:错误:'console'未定义 [英] Common idiom to avoid IE throw: Error: 'console' is undefined

查看:361
本文介绍了避免IE throw的常见习语:错误:'console'未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我安装了firebug,我写了所有这些日志语句。

I've installed firebug and I wrote all these log statements.

我在IE中测试了我的应用程序,当然我有undefined错误。

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.

我不知道该怎么办。

推荐答案

我通常做一个包装函数:

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 throw的常见习语:错误:'console'未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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