一個奇葩的最短的 IE 版本判斷JS指令碼

來源:互聯網
上載者:User

使用 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
嗯,大概就是這樣。挺有趣的。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.