欢迎来到代码驿站!

JavaScript代码

当前位置:首页 > 网页前端 > JavaScript代码

一个奇葩的最短的 IE 版本判断JS脚本

时间:2021-06-26 08:46:36|栏目:JavaScript代码|点击:

使用 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 = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->',
        all[0]
    );

    return v > 4 ? v : undef;

}());

注意一下这个 while 语句。是我觉得最有趣的。对于逗号操作符。我也不熟悉,还只是停留在像变量定义的用法上。比如:

复制代码 代码如下:

var  a= 'b', c = 'd', e = 'f';

var obj = {
 a: 'b',
 c: 'd',
 e: 'f'
}


问了工友 @kangpangpang,再查了一下书。其实这个比较少见。通常是返回最后一个值。
复制代码 代码如下:

var a = (1,2,3,5,6,0,9,4); // a === 4

嗯,大概就是这样。挺有趣的。

上一篇:Javascript中的 “&” 和 “|” 详解

栏    目:JavaScript代码

下一篇:加速IE的Javascript document输出的方法

本文标题:一个奇葩的最短的 IE 版本判断JS脚本

本文地址:http://www.codeinn.net/misctech/148319.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有