IE 10 およびその他の IE バージョン用の特定のコード パスがいくつかあります。IE10 が互換モードで実行されている場合、ブラウザーのバージョンは 7.0 に設定されます。JavaScript/JQuery を使用して、標準/互換モードに関係なく、IE 10 であるかどうかを検出する方法はありますか?
24451 次
4 に答える
21
navigator.userAgent
たとえば、文字列を使用してこれを検出できます。
「Mozilla/4.0 (互換性; MSIE 7.0; Windows NT 6.2; WOW64; Trident/6.0 ; .NET4.0E; .NET4.0C; InfoPath.3; .NET CLR 3.5.30729; .NET CLR 2.0.50727; .NET CLR 3.0.30729; Zune 4.7)"
Trident/6.0 は IE10 を意味します
MSIE 7.0 は互換モードを意味します
于 2013-02-11T09:32:01.313 に答える
12
これにより、MSIE の検出互換モードが機能するはずです。
iecheck.js
function trueOrFalse() {
return true;
}
function IeVersion() {
//Set defaults
var value = {
IsIE: false,
TrueVersion: 0,
ActingVersion: 0,
CompatibilityMode: false
};
//Try to find the Trident version number
var trident = navigator.userAgent.match(/Trident\/(\d+)/);
if (trident) {
value.IsIE = true;
//Convert from the Trident version number to the IE version number
value.TrueVersion = parseInt(trident[1], 10) + 4;
}
//Try to find the MSIE number
var msie = navigator.userAgent.match(/MSIE (\d+)/);
if (msie) {
value.IsIE = true;
//Find the IE version number from the user agent string
value.ActingVersion = parseInt(msie[1]);
} else {
//Must be IE 11 in "edge" mode
value.ActingVersion = value.TrueVersion;
}
//If we have both a Trident and MSIE version number, see if they're different
if (value.IsIE && value.TrueVersion > 0 && value.ActingVersion > 0) {
//In compatibility mode if the trident number doesn't match up with the MSIE number
value.CompatibilityMode = value.TrueVersion != value.ActingVersion;
}
return value;
}
iecheck.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing IE Compatibility Mode</title>
<script src="iecheck.js" type="text/javascript"></script>
</head>
<body>
<div id="results">Results: </div>
</br>
<script type="text/javascript">
var ie = IeVersion();
document.write("IsIE: " + ie.IsIE + "</br>");
document.write("TrueVersion: " + ie.TrueVersion + "</br>");
document.write("ActingVersion: " + ie.ActingVersion + "</br>");
document.write("CompatibilityMode: " + ie.CompatibilityMode + "</br>");
</script>
</body>
</html>
于 2015-08-07T13:46:54.150 に答える
6
ユーザー エージェント文字列の Trident 値は、実行中の IE の実際のバージョンを示します。
于 2013-02-19T02:34:36.317 に答える
5
これは私がJQuery .readyから使用するものです
$(document).ready(function () {
var iec = new IECompatibility();
alert('IsIE: ' + iec.IsIE + '\nVersion: ' + iec.Version + '\nCompatability On: ' + iec.IsOn);
});
function IECompatibility() {
var agentStr = navigator.userAgent;
this.IsIE = false;
this.IsOn = undefined; //defined only if IE
this.Version = undefined;
if (agentStr.indexOf("MSIE 7.0") > -1) {
this.IsIE = true;
this.IsOn = true;
if (agentStr.indexOf("Trident/6.0") > -1) {
this.Version = 'IE10';
} else if (agentStr.indexOf("Trident/5.0") > -1) {
this.Version = 'IE9';
} else if (agentStr.indexOf("Trident/4.0") > -1) {
this.Version = 'IE8';
} else {
this.IsOn = false; // compatability mimics 7, thus not on
this.Version = 'IE7';
}
} //IE 7
}
ベン・ホブグッド
于 2013-08-29T21:24:09.233 に答える