这篇文章主要介绍了一个奇葩的最短的 IE 版本判断 JS 脚本, 非常酷的一段代码, 需要的朋友可以参考下
Javascript 是一种由 Netscape 的 LiveScript 发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言,比如 Perl,遗留的速度问题,为客户提供更流畅的浏览效果。
使用 conditional comment 来判断 IE 的版本。嗯,是早早有人提出,但没有认真看代码。昨天刚好在看 CSS3 PIE 的时候看到,觉得是不是不靠谱。今天看到 Paul Irish 也提起,那么,推荐一下吧。这是作者博客上写的:
- // ----------------------------------------------------------
- // A short snippet for detecting versions of IE in JavaScript
- // without resorting to user-agent sniffing
- // ----------------------------------------------------------
- // If you're not in IE (or IE version is less than 5) then:
- // ie === undefined
- // If you're in IE (>=5) then you can determine which version:
- // ie === 7; // IE7
- // Thus, to detect IE:
- // if (ie) {}
- // And to detect the version:
- // ie === 6 // IE6
- // ie > 7 // IE8, IE9 ...
- // ie < 9 // Anything less than IE9
- // ----------------------------------------------------------
// UPDATE: Now using Live NodeList idea from @jdalton
var ie = (function(){
var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while (
div.innerhtml = '',
all[0]
);
return v > 4 ? v : undef;
}());
来源: