js判斷ie版本號碼的簡單實現代碼_javascript技巧

來源:互聯網
上載者:User

jQuery 2.0 去除了對瀏覽器版本號碼的判斷(它推薦特性檢測),這裡是一個老外寫的原生判斷方法,這段代碼著實巧妙!既簡介、有向後相容!一般做法都是:正則搜尋 USER_AGENT ;
但因為曆史原因, USER_AGENT 一直都不準確,而且被各大廠商改的亂七八糟。:
比如:

IE10 : Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)

IE11 : Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 11.0) like Gecko

這都是些什麼亂七八糟的什麼字串啊。。。鬼知道 IE12會變成什麼!!!
所以使用特性檢測、或者USER-AGENT檢測既不可靠,也不向後相容!

強烈推薦以下代碼!

複製代碼 代碼如下:

var _IE = (function () {
            var 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 : false;
        } ());
        alert(_IE);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.