function msieversion()
{
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0) // If Internet Explorer, return version number
{
alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
}
else // If another browser, return 0
{
alert('otherbrowser');
}
return false;
}
function msieversion() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer, return version number
{
alert(parseInt(ua.substring(msie + 5, ua.indexOf(".", msie))));
}
else // If another browser, return 0
{
alert('otherbrowser');
}
return false;
}
这是几年后,Edge浏览器现在使用Chromium作为其渲染引擎。 . b
遗憾的是,检查IE 11仍然是一件事。< / p >
这里有一个更直接的方法,因为古老版本的IE应该消失了。
if (window.document.documentMode) {
// Do IE stuff
}
以下是我以前的答案(2014年):
在Edge中用户代理字符串已经改变。
/**
* detect IEEdge
* returns version of IE/Edge or false, if browser is not a Microsoft browser
*/
function detectIEEdge() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// Edge => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
示例用法:
alert('IEEdge ' + detectIEEdge());
ie10默认字符串:
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
IE 11默认字符串:
Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko
Edge 12的默认字符串:
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0
Edge 13的默认字符串(thx @DrCord):
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586
Edge 14的默认字符串:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/14.14300
Edge 15的默认字符串:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063
Edge 16的默认字符串:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299
Edge 17的默认字符串:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134
Edge 18的默认字符串(内部预览):
Mozilla/5.0 (Windows NT 10.0; Win64; x64; ServiceUI 14) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/18.17730
var isIE = false;
var ua = window.navigator.userAgent;
var old_ie = ua.indexOf('MSIE ');
var new_ie = ua.indexOf('Trident/');
if ((old_ie > -1) || (new_ie > -1)) {
isIE = true;
}
if ( isIE ) {
//IE specific code goes here
}
更新1:一个更好的方法
我现在就推荐这个。它仍然是非常可读的,并且代码更少:)
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);
if ( isIE ) {
//IE specific code goes here
}
感谢JohnnyFun在评论中给出的简短答案:)
更新2:在CSS中测试IE
首先,如果可以的话,应该使用@supports语句而不是JS来检查浏览器是否支持某种CSS特性。
.element {
/* styles for all browsers */
}
@supports (display: grid) {
.element {
/* styles for browsers that support display: grid */
}
}
(注意IE根本不支持@supports,并且会忽略@supports语句中的任何样式。)
如果这个问题不能用@supports解决,那么你可以这样做:
// JS
var ua = window.navigator.userAgent;
var isIE = /MSIE|Trident/.test(ua);
if ( isIE ) {
document.documentElement.classList.add('ie')
}
/* CSS */
.element {
/* styles that apply everywhere */
}
.ie .element {
/* styles that only apply in IE */
}
function isIE()
{
var rv = -1;
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
else if (navigator.appName == 'Netscape')
{
var ua = navigator.userAgent;
var re = new RegExp("Trident/.*rv:([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv == -1 ? false: true;
}
< p > 方法1: < br >
美元。浏览器在jQuery 1.3版本中已弃用,并在1.9中被移除
if ( $.browser.msie) {
alert( "Hello! This is IE." );
}
< p > 方法2: < br >
.使用条件注释
<!--[if gte IE 8]>
<p>You're using a recent version of Internet Explorer.</p>
<![endif]-->
<!--[if lt IE 7]>
<p>Hm. You should upgrade your copy of Internet Explorer.</p>
<![endif]-->
<![if !IE]>
<p>You're not using Internet Explorer.</p>
<![endif]>
方法03: < br >
/**
* Returns the version of Internet Explorer or a -1
* (indicating the use of another browser).
*/
function getInternetExplorerVersion()
{
var rv = -1; // Return value assumes failure.
if (navigator.appName == 'Microsoft Internet Explorer')
{
var ua = navigator.userAgent;
var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (re.exec(ua) != null)
rv = parseFloat( RegExp.$1 );
}
return rv;
}
function checkVersion()
{
var msg = "You're not using Internet Explorer.";
var ver = getInternetExplorerVersion();
if ( ver > -1 )
{
if ( ver >= 8.0 )
msg = "You're using a recent copy of Internet Explorer."
else
msg = "You should upgrade your copy of Internet Explorer.";
}
alert( msg );
}
< p > 方法04: < br >
使用JavaScript/手动检测
/*
Internet Explorer sniffer code to add class to body tag for IE version.
Can be removed if your using something like Modernizr.
*/
var ie = (function ()
{
var undef,
v = 3,
div = document.createElement('div'),
all = div.getElementsByTagName('i');
while (
div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i>< ![endif]-->',
all[0]);
//append class to body for use with browser support
if (v > 4)
{
$('body').addClass('ie' + v);
}
}());
function msieversion() {
var ua = window.navigator.userAgent;
console.log(ua);
var msie = ua.indexOf("MSIE ");
if (msie > -1 || navigator.userAgent.match(/Trident.*rv:11\./)) {
// If Internet Explorer, return version numbe
// You can do what you want only in IE in here.
var version_number=parseInt(ua.substring(msie + 5, ua.indexOf(".", msie)));
if (isNaN(version_number)) {
var rv_index=ua.indexOf("rv:");
version_number=parseInt(ua.substring(rv_index+3,ua.indexOf(".",rv_index)));
}
console.log(version_number);
} else {
//other browser
console.log('otherbrowser');
}
}
/ detect IE
var IEversion = detectIE();
if (IEversion !== false) {
document.getElementById('result').innerHTML = 'IE ' + IEversion;
} else {
document.getElementById('result').innerHTML = 'NOT IE';
}
// add details to debug result
document.getElementById('details').innerHTML = window.navigator.userAgent;
/**
* detect IE
* returns version of IE or false, if browser is not Internet Explorer
*/
function detectIE() {
var ua = window.navigator.userAgent;
// Test values; Uncomment to check result …
// IE 10
// ua = 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)';
// IE 11
// ua = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko';
// IE 12 / Spartan
// ua = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 Edge/12.0';
// Edge (IE 12+)
// ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586';
var msie = ua.indexOf('MSIE ');
if (msie > 0) {
// IE 10 or older => return version number
return parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {
// IE 11 => return version number
var rv = ua.indexOf('rv:');
return parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
}
var edge = ua.indexOf('Edge/');
if (edge > 0) {
// Edge (IE 12+) => return version number
return parseInt(ua.substring(edge + 5, ua.indexOf('.', edge)), 10);
}
// other browser
return false;
}
var msie, // holds major version number for IE, or NaN if UA is not IE.
// Support: IE 9-11 only
/**
* documentMode is an IE-only property
* http://msdn.microsoft.com/en-us/library/ie/cc196988(v=vs.85).aspx
*/
msie = window.document.documentMode;
然后有几行代码分散在使用它作为一个数字,如
if (event === 'input' && msie <= 11) return false;
function ieVersion(uaString) {
uaString = uaString || navigator.userAgent;
var match = /\b(MSIE |Trident.*?rv:|Edge\/)(\d+)/.exec(uaString);
if (match) return parseInt(match[2])
}
function isCrappyIE() {
var ua = window.navigator.userAgent;
var crappyIE = false;
var msie = ua.indexOf('MSIE ');
if (msie > 0) {// IE 10 or older => return version number
crappyIE = true;
}
var trident = ua.indexOf('Trident/');
if (trident > 0) {// IE 11 => return version number
crappyIE = true;
}
return crappyIE;
}
if(!isCrappyIE()){console.table('not a crappy browser);}
if (document.documentMode)
{
console.log('Hello Microsoft IE User!');
}
if (!document.documentMode && window.msWriteProfilerMark) {
console.log('Hello Microsoft Edge User!');
}
if (document.documentMode || window.msWriteProfilerMark)
{
console.log('Hello Microsoft User!');
}
if (window.msWriteProfilerMark)
{
console.log('Hello Microsoft User in fewer characters!');
}
此外,这将检测新的Chredge/Edgium(阿纳海姆):
function isEdg()
{
for (var i = 0, u="Microsoft", l =u.length; i < navigator.plugins.length; i++)
{
if (navigator.plugins[i].name != null && navigator.plugins[i].name.substr(0, l) === u)
return true;
}
return false;
}
这个可以检测铬:
function isChromium()
{
for (var i = 0, u="Chromium", l =u.length; i < navigator.plugins.length; i++)
{
if (navigator.plugins[i].name != null && navigator.plugins[i].name.substr(0, l) === u)
return true;
}
return false;
}