IE8中的控制台未定义问题 [英] Console undefined issue in IE8

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

问题描述

据我所知,如果调试窗口打开,IE只会将 console 视为对象。如果调试窗口未打开,它会将控制台视为未定义。

I understand that IE only treats console as an object if the debug window is open. If the debug window is not open, it treats console as undefined.

这就是我决定添加 if 检查的原因:

Which is why I decided to add an if check like this :

        if(console)
            console.log('removing child');

我的理解是,如果 console 未定义,它将跳过的console.log 即可。但是在IE8中, if(控制台)行通过,我在 console.log 之前得到了一个未定义的异常。这很奇怪。

My understanding is that if console is undefined it will skip the console.log. However in IE8 the if(console) line passes and I get an undefined exception like before at console.log. This is weird.

有没有解决方法呢?
以及如何在代码中编写控制台以便在所有三种浏览器上运行?

Is there a way around this? and how do you code console in your code so that it runs on all three browsers?

推荐答案

你可以添加以下if子句:

You could add the following to the if clause:

if (console && console.log) {
    console.log('removing child');
}

或者在console.log函数周围写一个日志包装器。

Or write a log wrapper around the console.log function like this.

window.log = function () {
    if (this.console && this.console.log) {
        this.console.log(Array.prototype.slice.call(arguments));
    }
}

像这样使用:

log("This method is bulletproof", window, arguments");

这里有一个jsfiddle:
http: //jsfiddle.net/joquery/4Ugvg/

And here is a jsfiddle for this: http://jsfiddle.net/joquery/4Ugvg/

这篇关于IE8中的控制台未定义问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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