簡體   English   中英

如何僅使用JavaScript定位Internet Explorer 11?

[英]How can I target only Internet Explorer 11 with JavaScript?

使用JavaScript定位IE11的最不容易出錯的方法是什么?

注意:這應該僅用於分析或通知用戶他們正在使用的瀏覽器。 對於其他一切,都有功能檢測。

IE 11的User-agent字符串當前是這樣的:

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

Windows 10示例:

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

這意味着您可以簡單地測試版本11.xx,

var isIE11 = /Trident.*rv[ :]*11\./.test(navigator.userAgent);

作為IE10用戶代理

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

打賭現在Trident/X應該是真正的版本,這可能也是安全的。

IE11在它的UA字符串中保留“Trident”,但是刪除了MSIE。 一種檢測瀏覽器的簡單方法是IE11或以上(IE12,IE13等):

var isAtLeastIE11 = !!(navigator.userAgent.match(/Trident/) && !navigator.userAgent.match(/MSIE/));

如果您想要IE11(並且您不希望將來的IE版本匹配),請執行以下操作:

var isIE11 = !!(navigator.userAgent.match(/Trident/) && navigator.userAgent.match(/rv[ :]11/));
var isIE11 = !!navigator.userAgent.match(/Trident\/7.0; rv 11/);

資料來源: http//www.nczonline.net/blog/2013/07/02/internet-explorer-11-dont-call-me-ie/

我使用以下模式來定位所有IE瀏覽器。 如果您只需要IE 11,可以將其縮短。

 /msie|trident|edge/g.test(navigator.userAgent.toLowerCase());

祝好運!

弗雷德里克

試試這個,

navigator.sayswho= (function(){
   var N= navigator.appName, ua= navigator.userAgent, tem;
   var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
   if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
   M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
   return M;
})();

來自JavaScript中的瀏覽器檢測?

針對IE = 11進行了更新

用這個

var isIE11 = navigator.userAgent.match(/Trident\/7.0; rv 11.0/);

閱讀此http://msdn.microsoft.com/en-us/library/ie/bg182625%28v=vs.85%29.aspx

這將設置ie到IE的版本,如果沒有則設置為0。 它適用於1到11,但如果Microsoft刪除Trident引擎,可能無法檢測到未來的版本。

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

您還可能有興趣在我相關,更詳細的答案在這里

這是一個可用於檢測任何瀏覽器的腳本:

<script>

  // Opera
  var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;

  // Firefox 1.0+
  var isFirefox = typeof InstallTrigger !== 'undefined';

  // Safari 3.0+ "[object HTMLElementConstructor]" 
  var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);

  // Internet Explorer 6-11
  var isIE = /*@cc_on!@*/false || !!document.documentMode;

  // Edge 20+
  var isEdge = !isIE && !!window.StyleMedia;

  // Chrome 1+
  var isChrome = !!window.chrome && !!window.chrome.webstore;

  // Blink engine detection
  var isBlink = (isChrome || isOpera) && !!window.CSS;

  if (isFirefox==true) {
    alert(isFirefox)
    $('.container-fluid').css({"overflow-y":"auto","height":"150%"});  
  }

</script>

暫無
暫無

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

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