判断IE浏览器版本
IE版本代码总结
IE版本 | 支持的状态 |
---|---|
10及以下 | document.all |
9及以下 | document.all && !window.atob |
8及以下 | document.all && !document.addEventListener |
7及以下 | document.all && !document.querySelector |
6及以下 | document.all && !window.XMLHttpRequest |
5.x | document.all && !document.compatMode |
例子
下面的条件代码只会在IE7及一下浏览器中运行
if (document.all && !document.querySelector) {
alert('IE7 or lower');
}
if (document.all && document.querySelector && !document.addEventListener) {
alert('IE8');
}
下面的条件代码当浏览器为IE11+ 或者非IE时为真
//code from http://caibaojian.com/detect-ie-version.html
if (!document.all) {
alert('IE11+ or not IE');
}
IE11或者非IE
if (!document.all) { alert('IE11+ or not IE'); }
IE10
if (document.all && document.addEventListener && window.atob) { alert('IE10'); }
IE9
if (document.all && document.addEventListener && !window.atob) { alert('IE9'); }
IE8上面已经给出
if (document.all && document.querySelector && !document.addEventListener) { alert('IE8'); }
IE7
if (document.all && window.XMLHttpRequest && !document.querySelector) { alert('IE7'); }
IE6
if (document.all && document.compatMode && !window.XMLHttpRequest) { alert('IE6'); }
检测IE版本
var win = window; var doc = win.document; var input = doc.createElement ("input"); var ie = (function (){ //"!win.ActiveXObject" is evaluated to true in IE11 if (win.ActiveXObject === undefined) return null; if (!win.XMLHttpRequest) return 6; if (!doc.querySelector) return 7; if (!doc.addEventListener) return 8; if (!win.atob) return 9; //"!doc.body.dataset" is faster but the body is null when the DOM is not //ready. Anyway, an input tag needs to be created to check if IE is being //emulated if (!input.dataset) return 10; return 11; })();