javascript – IE8中控制台未定义的问题
作者:互联网
参见英文答案 > ‘console’ is undefined error for Internet Explorer 21个
据我所知,如果调试窗口打开,IE仅将控制台视为对象.如果调试窗口未打开,则将控制台视为未定义.
这就是为什么我决定添加if检查这样的原因:
if(console)
console.log('removing child');
我的理解是,如果控制台未定义,它将跳过console.log.但是在IE8中if(控制台)行通过,我在console.log之前得到了一个未定义的异常.这很奇怪.
有没有解决的办法?
以及如何在代码中编写控制台以便它在所有三个浏览器上运行?
解决方法:
您可以将以下内容添加到if子句:
if (console && console.log) {
console.log('removing child');
}
或者像这样在console.log函数周围写一个日志包装器.
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/
标签:javascript,internet-explorer-8 来源: https://codeday.me/bug/20191009/1875701.html