js 判斷瀏覽器

來源:互聯網
上載者:User

標籤:

var isIE=!!window.ActiveXObject;
var isIE6=isIE&&!window.XMLHttpRequest;
var isIE8=isIE&&!!document.documentMode;
var isIE7=isIE&&!isIE6&&!isIE8;
if (isIE){
if (isIE6){
alert("ie6");
}else if (isIE8){
alert("ie8");
}else if (isIE7){
alert("ie7");
}
} //判斷ie和非iefunction isIe(){    return ("ActiveXObject" in window); } //判斷IE6瀏覽器從IE7開始IE是支援XMLHttpRequest對象的,唯獨IE6是不支援的。根據這個特性和前面判斷IE的函數isIe()我們就知道怎麼判斷IE6了吧。判斷方法如下function isIe6() {   // ie6是不支援window.XMLHttpRequest的    return isIe() && !window.XMLHttpRequest; } //判斷IE7瀏覽器因為從IE8開始是支援文檔模式的,它支援document.documentMode。IE7是不支援的,但是IE7是支援XMLHttpRequest對象的。判斷方法如下function isIe7() {   //只有IE8+才支援document.documentMode   return isIe() && window.XMLHttpRequest && !document.documentMode; } //判斷IE8瀏覽器在從IE9開始,微軟慢慢的靠近標準,我們把IE678稱為非標準瀏覽器,IE9+與其他如chrome,firefox瀏覽器稱為標準瀏覽器。兩者的區別其中有一個是。大家測試下如下代碼。返回的是什麼alert(-[1,]);//在IE678中列印的是NaN,但是在標準瀏覽器列印的是-1那麼我們就可以根據上面的區別來判斷是IE8瀏覽器。方法如下function isIe8(){   // alert(!-[1,])//->IE678返回NaN 所以!NaN為true 標準瀏覽器返回-1 所以!-1為false  return isIe() &&!-[1,]&&document.documentMode;}//判斷IE678isIe678 = !-[1,] //判斷IE9、IE10、IE11瀏覽器從IE8瀏覽器是支援JSON內建對象的,從IE10開始支援js的strict 模式,關於JS中的strict 模式請參考這篇文章http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.htmlIE9+下alert(!-[1,])返回的是false,IE9+是支援addEventListener的,但是IE11瀏覽器中是不支援原來IE中專屬的事件綁定attachEvent。根據這些區別我們就能區分出IE9、IE10、IE11瀏覽器了。 //判斷其他瀏覽器var check = function(r) {        return r.test(navigator.userAgent.toLowerCase()); };var statics = {        /**         * 是否為webkit核心的瀏覽器         */        isWebkit : function() {          return check(/webkit/);        },        /**         * 是否為Firefox瀏覽器         */        isFirefox : function() {          return check(/firefox/);        },        /**         * 是否為Google瀏覽器         */        isChrome : function() {          return !statics.isOpera() && check(/chrome/);        },        /**         * 是否為Opera瀏覽器         */        isOpera : function() {          return check(/opr/);        },        /**         * 檢測是否為Safari瀏覽器         */        isSafari : function() {          // google chrome瀏覽器中也包含了safari          return !statics.isChrome() && !statics.isOpera() && check(/safari/);        }};

js 判斷瀏覽器

聯繫我們

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