簡體   English   中英

如何檢測IE11?

[英]How to detect IE11?

當我想檢測IE時,請使用以下代碼:

function getInternetExplorerVersion()
{
  var rv = -1;
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}
function checkVersion()
{
  var msg = "You're not using Internet Explorer.";
  var ver = getInternetExplorerVersion();

  if ( ver > -1 )
  {
    msg = "You are using IE " + ver;
  }
  alert( msg );
}

但是IE11返回“您沒有使用Internet Explorer”。 如何檢測到它?

IE11不再報告為MSIE ,根據此更改列表,有意避免誤檢測。

如果您真的想知道它是IE,該怎么辦是如果navigator.appName返回Netscape ,則在用戶代理中檢測Trident/字符串,類似(未經測試);

 function getInternetExplorerVersion() { var rv = -1; if (navigator.appName == 'Microsoft Internet Explorer') { var ua = navigator.userAgent; var re = new RegExp("MSIE ([0-9]{1,}[\\\\.0-9]{0,})"); if (re.exec(ua) != null) rv = parseFloat( RegExp.$1 ); } else if (navigator.appName == 'Netscape') { var ua = navigator.userAgent; var re = new RegExp("Trident/.*rv:([0-9]{1,}[\\\\.0-9]{0,})"); if (re.exec(ua) != null) rv = parseFloat( RegExp.$1 ); } return rv; } console.log('IE version:', getInternetExplorerVersion()); 

請注意,IE11(afaik)仍處於預覽狀態,用戶代理可能會在發布前更改。

!(window.ActiveXObject) && "ActiveXObject" in window使用!(window.ActiveXObject) && "ActiveXObject" in window來顯式檢測IE11。

若要檢測任何IE(pre-Edge,“ Trident”)版本,請"ActiveXObject" in window使用"ActiveXObject" in window

MSInputMethodContext用作功能檢測檢查的一部分。 例如:

//Appends true for IE11, false otherwise
window.location.hash = !!window.MSInputMethodContext && !!document.documentMode;

參考文獻

我已經閱讀了您的答案,並進行了混音。 它似乎適用於Windows XP(IE7 / IE8)和Windows 7(IE9 / IE10 / IE11)。

function ie_ver(){  
    var iev=0;
    var ieold = (/MSIE (\d+\.\d+);/.test(navigator.userAgent));
    var trident = !!navigator.userAgent.match(/Trident\/7.0/);
    var rv=navigator.userAgent.indexOf("rv:11.0");

    if (ieold) iev=new Number(RegExp.$1);
    if (navigator.appVersion.indexOf("MSIE 10") != -1) iev=10;
    if (trident&&rv!=-1) iev=11;

    return iev;         
}

當然,如果我返回0,則表示沒有IE。

從用戶代理獲取IE版本

var ie = 0;
try { ie = navigator.userAgent.match( /(MSIE |Trident.*rv[ :])([0-9]+)/ )[ 2 ]; }
catch(e){}

工作原理: 所有IE版本的用戶代理字符串包含“ MSIE 空間 版本 ”或“ Trident 其他文本 rv 空間或冒號 版本 ”部分。 知道了這一點,我們從String.match()正則表達式中獲取版本號。 try-catch塊用於縮短代碼,否則我們需要測試非IE瀏覽器的數組范圍。

注意:可以欺騙或省略用戶代理,如果用戶已將其瀏覽器設置為“兼容模式”,則有時是無意的。 盡管在實際中這似乎並不是一個大問題。


在沒有用戶代理的情況下獲取IE版本

var d = document, w = window;
var ie = ( !!w.MSInputMethodContext ? 11 : !d.all ? 99 : w.atob ? 10 : 
d.addEventListener ? 9 : d.querySelector ? 8 : w.XMLHttpRequest ? 7 : 
d.compatMode ? 6 : w.attachEvent ? 5 : 1 );

工作原理: IE的每個版本都增加了對以前版本中未提供的其他功能的支持。 因此,我們可以自上而下地測試功能。 為了簡潔起見,此處使用三元序列,不過if-thenswitch語句也可以正常工作。 變量ie設置為5-11的整數,較舊的則設置為1,較新的/非IE則設置為99。 如果只想測試IE 1-11,則可以將其設置為0。

注意:如果您的代碼在帶有第三方腳本的頁面上運行,則對象檢測可能會中斷,這些腳本為諸如document.addEventListener類的document.addEventListener添加了polyfills。 在這種情況下,用戶代理是最佳選擇。


檢測瀏覽器是否為現代

如果您僅對瀏覽器是否支持大多數HTML 5和CSS 3標准感興趣,則可以合理地假設 IE 8和更低版本仍然是主要的問題應用程序。 測試window.getComputedStyle還將為您提供相當不錯的現代瀏覽器組合(IE 9,FF 4,Chrome 11,Safari 5,Opera 11.5)。 IE 9大大改進了對標准的支持,但是原生CSS動畫需要IE 10。

var isModernBrowser = ( !document.all || ( document.all && document.addEventListener ) ); 

Angular JS就是這樣。

msie = parseInt((/msie (\d+)/.exec(navigator.userAgent.toLowerCase()) || [])[1]);
if (isNaN(msie)) {
  msie = parseInt((/trident\/.*; rv:(\d+)/.exec(navigator.userAgent.toLowerCase()) || [])[1]);
}

如果其他瀏覽器(如chrome,firefox)的IE和NaN,則msie將為正數。

為什么呢?

從Internet Explorer 11開始,用戶代理字符串已發生重大變化。

請參考:

msdn#1 msdn#2

解決方案:

 function GetIEVersion() { var sAgent = window.navigator.userAgent; var Idx = sAgent.indexOf("MSIE"); // If IE, return version number. if (Idx > 0) return parseInt(sAgent.substring(Idx+ 5, sAgent.indexOf(".", Idx))); // If IE 11 then look for Updated user agent string. else if (!!navigator.userAgent.match(/Trident\\/7\\./)) return 11; else return 0; //It is not IE } if ((GetIEVersion() > 0) || (navigator.userAgent.toLowerCase().indexOf('firefox') > -1)){ alert("This is IE " + GetIEVersion()); }else { alert("This no is IE "); } 

我正在使用一種更簡單的方法:

導航器全局對象具有屬性接觸點,在Internet Exlorer 11中稱為msMaxTouchPoints。

因此,如果您尋找:

navigator.msMaxTouchPoints !== void 0 

您會找到Internet Explorer 11。

var ua = navigator.userAgent.toString().toLowerCase();
var match = /(trident)(?:.*rv:([\w.]+))?/.exec(ua) ||/(msie) ([\w.]+)/.exec(ua)||['',null,-1];
var rv = match[2];
return rv;

嘗試這個:

var trident = !!navigator.userAgent.match(/Trident\/7.0/);
var net = !!navigator.userAgent.match(/.NET4.0E/);
var IE11 = trident && net
var IEold = ( navigator.userAgent.match(/MSIE/i) ? true : false );
if(IE11 || IEold){
alert("IE")
}else{
alert("Other")
}

這似乎是一個更好的方法。 如果沒有匹配項,“ indexOf”返回-1。 它不會覆蓋主體上的現有類,而只是添加它們。

// add a class on the body ie IE 10/11
var uA = navigator.userAgent;
if(uA.indexOf('Trident') != -1 && uA.indexOf('rv:11') != -1){
    document.body.className = document.body.className+' ie11';
}
if(uA.indexOf('Trident') != -1 && uA.indexOf('MSIE 10.0') != -1){
    document.body.className = document.body.className+' ie10';
}

使用此檢測大多數瀏覽器:

var getBrowser = function(){
  var navigatorObj = navigator.appName,
      userAgentObj = navigator.userAgent,
      matchVersion;
  var match = userAgentObj.match(/(opera|chrome|safari|firefox|msie|trident)\/?\s*(\.?\d+(\.\d+)*)/i);
  if( match && (matchVersion = userAgentObj.match(/version\/([\.\d]+)/i)) !== null) match[2] = matchVersion[1];
  //mobile
  if (navigator.userAgent.match(/iPhone|Android|webOS|iPad/i)) {
    return match ? [match[1], match[2], mobile] : [navigatorObj, navigator.appVersion, mobile];
  }
  // web browser
  return match ? [match[1], match[2]] : [navigatorObj, navigator.appVersion, '-?'];
};

https://gist.github.com/earlonrails/5266945

我在帶有滾動條的元素上使用了onscroll事件。 在IE中觸發時,我添加了以下驗證:

onscroll="if (document.activeElement==this) ignoreHideOptions()"

僅適用於IE瀏覽器:

var ie = 'NotIE'; //IE5-11, Edge+
    if( !!document.compatMode ) {
        if( !("ActiveXObject" in window) ) ) ie = 'EDGE';
        if( !!document.uniqueID){
            if('ActiveXObject' in window && !window.createPopup ){ ie = 11; }
            else if(!!document.all){
                    if(!!window.atob){ie = 10;}
                    else if(!!document.addEventListener) {ie = 9;}
                    else if(!!document.querySelector){ie = 8;}
                    else if(!!window.XMLHttpRequest){ie = 7;}
                    else if(!!document.compatMode){ie = 6;}
                    else ie = 5;
                }
        }
    }

使用警報;

測試:

 var browserVersionExplorer = (function() { var ie = '<s>NotIE</s>', me = '<s>NotIE</s>'; if (/msie\\s|trident\\/|edge\\//i.test(window.navigator.userAgent) && !!(document.documentMode || document.uniqueID || window.ActiveXObject || window.MSInputMethodContext)) { if (!!window.MSInputMethodContext) { ie = !("ActiveXObject" in window) ? 'EDGE' : 11; } else if (!!document.uniqueID) { if (!!(window.ActiveXObject && document.all)) { if (document.compatMode == "CSS1Compat" && !!window.DOMParser ) { ie = !!window.XMLHttpRequest ? 7 : 6; } else { ie = !!(window.createPopup && document.getElementById) ? parseFloat('5.5') : 5; } if (!!document.documentMode && !!document.querySelector ) { ie = !!(window.atob && window.matchMedia) ? 10 : ( !!document.addEventListener ? 9 : 8); } } else ie = !!document.all ? 4 : (!!window.navigator ? 3 : 2); } } return ie > 1 ? 'IE ' + ie : ie; })(); alert(browserVersionExplorer); 

更新2017年6月1日

現在我們可以使用更簡單的方法:

var uA = window.navigator.userAgent,
    onlyIEorEdge = /msie\s|trident\/|edge\//i.test(uA) && !!( document.uniqueID || window.MSInputMethodContext),
    checkVersion = (onlyIEorEdge && +(/(edge\/|rv:|msie\s)([\d.]+)/i.exec(uA)[2])) || NaN;

坦率地說,我會說使用可以滿足您需求的庫(例如,platform.js)。 在某些時候,事情將會改變,並且庫將為這些改變做好准備,並且使用正則表達式進行手動解析將失敗。

謝天謝地IE消失了...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM