2017-10-17 42 views
3

我想檢查是否爲當前域啓用了IE11兼容性視圖。設置兼容性視圖是通過:工具>兼容性視圖設置。如何檢查IE11是否在使用JS的兼容性視圖中

我知道這已被要求由少數幾個年前,但看起來像answers不工作了,由於recent update on IE11.

有誰知道另一種方式來做到這一點?

+0

的可能的複製[如何檢測IE11?] (https://stackoverflow.com/questions/17907445/how-to-detect-ie11) –

+0

不完全相同。這是如何檢查IE 11的兼容模式。以上鍊接僅用於檢查IE 11版本。 – Jimbob

+0

好像你只需要在userAgent的開始處尋找'compatible' –

回答

0

如果你只是想檢查你是否在兼容模式下運行,你可以使用這個腳本。

//創建新對象ieUserAgent

var ieUserAgent = { 

init: function() { 

// Get the user agent string 

var ua = navigator.userAgent; 

this.compatibilityMode = false; 

    // alert (ua); 

    if(ua.indexOf("MSIE") == -1){ 

     this.version = 0; 

     return 0; 

    } 

    if(ua.indexOf("compatible") == -1){ 

     this.compatibilityMode = false; 

     return 0; 

    }else{ 

     this.compatibilityMode = true; 

     return 0; 

    } 

} 
}; 

// Initialize the ieUserAgent object 
ieUserAgent.init(); 

-OR-

/** * 檢查客戶端是IE和兼容視圖 * * @Returns {布爾} */

function isIECompatibilityMode() { 

    var ua = navigator.userAgent; 

    if (ua.indexOf("MSIE") == -1) { 

     return false; 

    } 

    return (ua.indexOf("compatible") != -1); } 
+1

感謝Janaki,但這與舊解決方案相同,我上面提到的IE11更新刪除了字符串「MSIE 「不管你是否在兼容性視圖中,所以這是行不通的。 – Byron

+0

請格式化您的答案。 – 2017-10-17 05:03:45

1

在IE版本8-11中您可以使用document.documentMode。有效值爲5,7(兼容模式),8,9,10和11(邊緣)。

  • 在控制檯中設置兼容模式直接更改值。

  • 加載一個網頁,一個<meta http-equiv標籤改變「工具 - >兼容性視圖設置 」值

  • 添加網站到兼容模式的值變化爲7

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

示例

例如,如果我在IE11加載這個頁面,我得到的11

<!doctype HTML> 
<body> 
<p>Hello World!<p> 
</body> 

documentMode此頁面加載IE11設置documentMode到9

<html> 
<head> 
<meta http-equiv="x-ua-compatible" content="IE=9"/> 
</head> 
<body> 
<p>Hello World!<p> 
</body> 
</html> 
+0

我想檢測兼容性視圖是否啓用兼容模式下的開發工具,我認爲你是指仿真選項卡。在IE11設置兼容性視圖是通過 - 工具>兼容性視圖設置 – Byron