![](/img/trans.png)
[英]How to Detect IE7/IE8 “run as administrator” mode with Javascript
[英]JavaScript: Can I detect IE9 if it's in IE7 or IE8 compatibility mode?
我需要知道通過用戶代理字符串將自己標識為 IE7 或 IE8 的瀏覽器是否真的是那些瀏覽器,或者它是否是 7 或 8 兼容模式下的 IE9。
從我在用戶代理字符串中可以看到,IE7 兼容模式下的 IE9 提供了與真正的 IE7 相同的字符串。 是否有一個額外的屬性/元素/對象可以測試以查看它是否是偽裝的“真的”IE9?
我認為文檔模式無濟於事,因為我的腳本加載到的頁面可能是強制怪癖或強制特定設置。
我希望 IE9 將具有一些存在且可測試的屬性,無論它是處於 7、8 還是 9 模式。
編輯添加...
好的,我知道我現在哪里出錯了。 我正在使用“瀏覽器模式”下拉菜單並將其切換到 IE8 和 IE7,並認為這分別是“IE8 兼容模式”和“IE7 兼容模式”。 這當然不是真的。 開發者工具的瀏覽器模式真的是把它切換到“像”那些舊的瀏覽器,所以報告原始的用戶代理字符串是正確的。
如果我將瀏覽器模式保留在 IE9 或 IE9 兼容性中並嘗試使用文檔模式下拉變體,那么實際上我會在所有 8 種組合(兩種瀏覽器模式和 4 種文檔模式)中得到“Trident/5.0”。 我只需要避免選擇瀏覽器模式 IE7 和 IE8,因為它們確實是(模擬的)IE7 和 IE8。
因此,頁面、非開發人員用戶、元標記或 Microsoft 的兼容性列表都無法將 IE9 放入這個無法識別的 state 中。
只需使用if(navigator.userAgent.indexOf("Trident/5")>-1)
就足夠了。
別擔心,這不適用於 styles、格式、邏輯或頁面內容。 我對這些事情使用特征檢測。 我只需要檢測 IE9(無論它處於何種模式)並對此做出非頁面內容決定。
感謝您通過您的建議和鏈接引導我找到答案。
實際上,在 IE7 兼容模式下運行時,IE9 的用戶代理字符串是不同的,因此這將是區分不同 IE 版本的最佳方法之一。
與 IE8 類似,IE9 的 Compatibility View 將 map 轉為 IE7 Standards Mode,在 Compatibility View 中 IE9 的 UA 字符串為:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/5.0)
在兼容性視圖中,IE9 通過應用程序版本號 (Mozilla/4.0) 和版本令牌 (MSIE 7.0) 將自身報告為 IE7。 這樣做是為了兼容性。 從 'Trident/4.0' 到 'Trident/5.0' 的遞增 Trident 令牌允許網站區分在 Compat View 中運行的 IE9 和在 Compat View 中運行的 IE8 。
(重點由我添加)。 因此,用戶代理字符串與它報告自己是“Mozilla/4.0”和 MSIE 7.0 相同,但 IE9 將始終是 Trident/5.0 - 無論它是 MSIE 7.0、MSIE 8.0 還是 MSIE 9.0。
實際上你應該看看這個很棒的編譯: Browser ID (User-Agent) Strings or even better useragentstrings.com
document.documentMode
是文檔模式的最佳方式。
IE7 不包含任何關於 Trident 的信息
User-Agent : Mozilla/4.0 (compatible; MSIE 7.0)
IE8 包含這個字符串:“Trident/4.0”
User-Agent : Mozilla/4.0 (compatible; MSIE 8.0; Trident/4.0)
IE9 包含這個字符串:“Trident/5.0”
兼容模式下的IE9:
User-Agent : Mozilla/4.0 (compatible; MSIE 7.0; Trident/5.0)
普通模式下的 IE9:
User-Agent : Mozilla/5.0 (compatible; MSIE 9.0; Trident/5.0)
我希望 IE9 將具有一些存在且可測試的屬性,無論它是處於 7、8 還是 9 模式。
檢查例如style.opacity ,它是在 IE9 中引入的,並且無論兼容模式如何都可用:
<![if IE]>
<script>
if(typeof document.documentElement.style.opacity!='undefined')
{
//this must be at least IE9
}
</script>
<![endif]>
有時需要從服務器變量中讀取用戶代理字符串,而不是從 javascript 導航器 object 中讀取。
比較差異:
ASP經典, IE11
client javascript, 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;BOIE9;ENUS) "
服務器 ASP, Request.ServerVariables("HTTP_USER_AGENT"): " Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; BOIE9;ENUS; rv:11.0) 像 Gecko "
ASP經典, IE11兼容模式:
client javascript, 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;BOIE9;ENUS)) "
server ASP, Request.ServerVariables("HTTP_USER_AGENT"): " Mozilla/4.0 (compatible; MSIE 7.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;媒體中心 PC 6.0;.NET4.0C;.NET4.0E;BOIE9;ENUS) "
來自https://stackoverflow.com/a/29288153/2879498
假設您有一個 ID 為 compat-warning 的隱藏元素:
Javascript 帶 jQuery:
$(function(){
function showCompatWarning() {
$('#compat-warning')
.css('display','block')
.css('height','auto')
.show();
}
var tridentOffset = navigator.appVersion.indexOf('Trident/');
if ( tridentOffset === -1 ) return;
var jscriptVersion = 0;
/*@cc_on @*/
/*@if (@_jscript) jscriptVersion = @_jscript_version ; @*/;
/*@end @*/
var tridentVersion = parseInt(navigator.appVersion.substr(tridentOffset+8),10);
var guessIEVersion = tridentVersion + 4;
if (( document.documentMode && jscriptVersion && jscriptVersion < 10 && jscriptVersion !== document.documentMode ) ||
( document.compatMode && document.compatMode === 'BackCompat') ||
( document.documentMode && document.documentMode < 10 && document.documentMode != guessIEVersion ))
showCompatWarning();
});
檢測和警告,您抵御兼容性地獄的第一道和最后一道防線。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.