簡體   English   中英

如何正確檢測IE11或更高版本?

[英]How to properly detect IE11 or later?

我正在使用IE 11運行Windows 7(64位) 在此處輸入圖片說明

具有以下navigator.userAgent

"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/7.0; SLCC2;
.NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0;
.NET4.0C; .NET4.0E)"

我希望能夠檢測IE的版本,然后才能在網站上顯示任何內容。 換句話說,我工作的公司已將大多數計算機更新為可以運行IE11或Chrome。 但是某些計算機仍具有IE9。

我希望我的網站能對運行IE11或Chrome的用戶正常運行。 應該檢測到任何其他版本的瀏覽器,並告知用戶更新其計算機。

我在SO上找到的所有代碼都將v11引用為userAgent字符串的一部分,但實際情況並非如此。

編輯:我也嘗試過:

var isIE11 = !!(navigator.userAgent.match(/Trident/) 
             && !navigator.userAgent.match(/MSIE/));  
         //value is false in IE6/IE9/IE11

var isIE11 = !!(navigator.userAgent.match(/Trident/) 
                 && navigator.userAgent.match(/rv 11/));   
          //value is false in IE6/IE9/IE11

var isIE11 = !(window.ActiveXObject) && "ActiveXObject" in window;
           //value is false in IE6/IE9/IE11

如何檢測IE11?

編輯2:此鏈接http://kangax.github.io/compat-table/es6/具有一種檢查僅在IE11上運行的hoisted ...功能的方法。 所以我也嘗試了這個: 在此處輸入圖片說明

{ function f() { return 1; } }
  function g() { return 1; }
{ function g() { return 2; } }
{ function h() { return 1; } }
  function h() { return 2; }

alert( f() === 1 && g() === 2 && h() === 1);  // alerts false in all ie versions

如果只有IE瀏覽器正在連接到頁面,則測試僅在IE11 +中支持的hoisted block-level function declaration

 function hoistTest() { // Note: only available outside of strict mode. { function f() { return 1; } } function g() { return 1; } { function g() { return 2; } } { function h() { return 1; } } function h() { return 2; } return f() === 1 && g() === 2 && h() === 1; } document.getElementById('out').appendChild(document.createTextNode('hoisted block-level function declaration: ' + hoistTest())); 
 <pre id="out"></pre> 

更新:在IE11上運行的屏幕截圖 在此處輸入圖片說明

這僅在IE11中返回true:

!(window.ActiveXObject) && "ActiveXObject" in window

檢查Trident引擎版本為7.0; Internet Explorer的早期版本具有Trident引擎的早期版本。

例子:

  • Internet Explorer 10具有Trident 6.0
  • Internet Explorer 9具有Trident 5.0

來源: https : //msdn.microsoft.com/en-us/library/hh869301(v=vs.85).aspx

請注意,對於非台式PC Internet Explorer版本(Lumia手機,Xbox等),您必須格外小心。

另外,Microsoft瀏覽器的最新版本Edge不再使用Trident版本。

在IE瀏覽器可以訪問documentMode物業document將返回IE的版本。

要測試11或更高版本,可以使用以下命令:

document.documentMode > 10

暫無
暫無

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

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