欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js ie非ie瀏覽器的幾種判斷方法小結

 更新時間:2021年07月27日 00:03:46   投稿:mdxy-dxy  
其實有很多判斷的方法,大都是根據(jù)瀏覽器的特性來的,這里簡單的整理下,需要的朋友可以參考下。

最近在 司徒正美js 全世界最短的IE瀏覽器判斷代碼 一文,看到只有6byte的判斷ie與非ie的方法。其代碼如下:

<script> 
if(!+[1,])alert("這是ie瀏覽器"); 
   else alert("這不是ie瀏覽器"); 
</script>

下面推薦一個比較好的支持ie11與edge的判斷代碼

function isIE() {
    if(!!window.ActiveXObject || "ActiveXObject" in window){
      return true;
    }else{
      return false;
   }
}

下面是具體的擴張,大家可以參考一下

var userAgent = navigator.userAgent; 
var isIE10 = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //ie11以下
var isEdge = userAgent.indexOf("Edge") > -1 && !isIE10;
var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;

if (isIE()){
	if(isIE11 || isEdge){
		//not run
		}else{
		alert("可以放百度廣告了,ie瀏覽器");
	}
}

其實有很多判斷的方法,大都是根據(jù)瀏覽器的特性來的。

  比如庫prototype的方法是:!!(window.attachEvent && navigator.userAgent.indexOf('Opera') === -1) 。就是根據(jù)ie支持window.attachEvent添加偵聽事件,非ie用window.addEventListener添加偵聽事件來判斷的。navigator.userAgent.indexOf('Opera') === -1是因為opara瀏覽器能偽裝成ie.如果!!(window.attachEvent )為真,就是ie;反之,如果!window.addEventListener為真,也可以判斷為ie.

  Ext使用的是!"1"[0],他利用IE無法使用數(shù)組下標訪問字符串的特性來判斷。在ie8下好像有問題。

在!+[1,]還未被發(fā)現(xiàn)前,判斷ie最短的表達式是 !+"\v1".它利用的是ie不支持垂直制表符的特性。

以前還有一個常用方法是document.all,由于opera瀏覽器能偽裝成ie??梢赃@樣寫:!!(document.all && navigator.userAgent.indexOf('Opera') === -1).

還有很多,先記這幾條,便于工作時查閱。

1.+[1,]
2.!+"\v1"
3.!!(window.attachEvent && navigator.userAgent.indexOf('Opera') === -1)
4.!!(!window.addEventListener&& navigator.userAgent.indexOf('Opera') === -1)
5.!!(document.all && navigator.userAgent.indexOf('Opera') === -1)

判斷IE瀏覽器的具體版本

function IEVersion() {
      var userAgent = navigator.userAgent; //取得瀏覽器的userAgent字符串  
      var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判斷是否IE<11瀏覽器  
      var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判斷是否IE的Edge瀏覽器  
      var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
      if(isIE) {
          var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
          reIE.test(userAgent);
          var fIEVersion = parseFloat(RegExp["$1"]);
          if(fIEVersion == 7) {
              return 7;
          } else if(fIEVersion == 8) {
              return 8;
          } else if(fIEVersion == 9) {
              return 9;
          } else if(fIEVersion == 10) {
              return 10;
          } else {
              return 6;//IE版本<=7
          }   
      } else if(isEdge) {
          return 'edge';//edge
      } else if(isIE11) {
          return 11; //IE11  
      }else{
          return -1;//不是ie瀏覽器
      }
  }

JS通過內(nèi)核判斷各種瀏覽器|區(qū)分360與谷歌(現(xiàn)在不能用了曾經(jīng)可以用)

function getBrowserInfo(){
        var ua = navigator.userAgent.toLocaleLowerCase();
        var browserType=null;
        if (ua.match(/msie/) != null || ua.match(/trident/) != null) {
            browserType = "IE";
            browserVersion = ua.match(/msie ([\d.]+)/) != null ? ua.match(/msie ([\d.]+)/)[1] : ua.match(/rv:([\d.]+)/)[1];
        } else if (ua.match(/firefox/) != null) {
            browserType = "火狐";
        }else if (ua.match(/ubrowser/) != null) {
            browserType = "UC";
        }else if (ua.match(/opera/) != null) {
            browserType = "歐朋";
        } else if (ua.match(/bidubrowser/) != null) {
            browserType = "百度";
        }else if (ua.match(/metasr/) != null) {
            browserType = "搜狗";
        }else if (ua.match(/tencenttraveler/) != null || ua.match(/qqbrowse/) != null) {
            browserType = "QQ";
        }else if (ua.match(/maxthon/) != null) {
            browserType = "遨游";
        }else if (ua.match(/chrome/) != null) {
            var is360 = _mime("type", "application/vnd.chromium.remoting-viewer");
            function _mime(option, value) {
                var mimeTypes = navigator.mimeTypes;
                for (var mt in mimeTypes) {
                    if (mimeTypes[mt][option] == value) {
                        return true;
                    }
                }
                return false;
            }
            if(is360){
                browserType = '360';
            }else{
                $('html').css("zoom",".80");
            }
        }else if (ua.match(/safari/) != null) {
            browserType = "Safari";
        }
}

只有原生Chrome中存在一種MimeType“application/vnd.chromium.remoting-viewer”,由此可以判斷瀏覽器是加殼Chrome或是原生Chrome。
再如,只有IE內(nèi)核的瀏覽器存在ActiveXObject對象。由此可以判斷是否為IE瀏覽器

判斷瀏覽器類型,我們需要遵循以下原則:
1、采取命中特征原則,當且僅當完全符合區(qū)分瀏覽器的特征時我們才會采用此特征。例如單純通過UA中MSIE來檢測是否為IE瀏覽器是不可靠的。而通過判斷是否存在MimeType“application/vnd.chromium.remoting-viewer”來斷言原生Chrome在現(xiàn)階段來看是可行性,但也不保證永久有效。
2、對于主流瀏覽器來說一般不存在UserAgent關鍵字沖突,但對于許多加殼瀏覽器者就不一定了。再次提出某數(shù)字瀏覽器,userAgent干脆和IE一模一樣,但渲染模式等等不不知道動了什么手腳,與標準IE行為差異很大。通過userAgent來判斷瀏覽器時,優(yōu)先命中瀏覽器特征字。匹配則基本確定為該瀏覽器,但未匹配也并不代表不是此瀏覽器。請悉知。
3、優(yōu)先使用瀏覽器特性來區(qū)分瀏覽器,因為這個準確性較高。其次再采用userAgent輔助判斷,從而達到最高的匹配度。
4、優(yōu)先檢測第三方加殼瀏覽器,目前并未有十分好的方案,只能枚舉大部分世面上存在并可以判斷的瀏覽器,其他未匹配任何規(guī)則的瀏覽器,為了兼容,請將規(guī)則落到四大瀏覽器之一。
5、判斷瀏覽器版本,僅僅是為了針對特定瀏覽器進行優(yōu)化,需要有特定的業(yè)務場景需要才要這么做?;蛘弋斈硞€瀏覽器出現(xiàn)兼容問題時,緊急添加針對瀏覽器的補丁代碼時才判斷。更加科學穩(wěn)妥的辦法是使用標準的JS函數(shù)和API,頁面元素和樣式設計遵循W3C標準。可能存在爭議的兼容性問題盡可能采用第三方框架如jQuery。這才是解決兼容性問題的根本。

相關文章

最新評論