检测浏览器控制台是否能够渲染颜色的任何方式? [英] Any way of detecting whether a browsers console is able to render colors?

查看:129
本文介绍了检测浏览器控制台是否能够渲染颜色的任何方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法检查浏览器是否能够使用控制台颜色而不嗅探?

Is there a way of checking to see if a browser is capable of using console colors without sniffing?

console.log('%c Oh my heavens! ', 'background: #222; color: #bada55');

例如,在chrome 26+和firebug将打印彩色输出。

For example that in chrome 26+ and firebug will print colored output.

推荐答案

这是以下几种情况之一,浏览器版本检测似乎是有效的方法。为了最小化这种方法的危险,确保使用黑名单,而不是白名单,无论这可能如何不直观的感觉现在(以确保你不留下新的未来浏览器,发生了很多旧的netscape为重点的代码)。我知道这不是你想听到的答案,但因为 console.log 是一个本地函数,它的效果可以没有观察到,到目前为止因为我可以看到唯一的选择是做浏览器版本检测。

This is one of these few cases where browser version detection seems the valid way to go. To minimize the dangers of this approach make sure to use a blacklist rather than a whitelist, no matter how unintuitive this might feel right now (to make sure you don't leave out new future browser as happened with a lot of old netscape focused code). I am aware that this isn't the answer you wanted to hear, but as console.log is a native function and it's effect can in no way be observed, so as far as I can see the only option is to do browser version detection.

这篇关于检测浏览器控制台是否能够渲染颜色的任何方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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