代码日记 | 小叉 | 2012年04月26日
var ieMode = document.documentMode;
var isIE = !!window.ActiveXObject;
var isIE6 = isIE && !window.XMLHttpRequest;
var isIE7 = isIE && !isIE6 && !ieMode || ieMode == 7;
var isIE8 = isIE && ieMode == 8;
var isIE9 = isIE && ieMode == 9;
if (isIE6) {
alert("正在使用 IE6 浏览器");
};
if (isIE7) {
alert("正在使用 IE7 浏览器,或者当前文档模式为 IE8");
};
if (isIE8) {
alert("正在使用 IE8 浏览器,或者当前文档模式为 IE8");
};
if (isIE9) {
alert("正在使用 IE9 浏览器,或者当前文档模式为 IE9");
};
在 IE8 之后,IE 添加了 document.documentMode 属性,在 IE7/IE6 及以前的版本中该值都会返回 undefined。具体变化可以参考下表:
浏览器 | IE6 | IE7 | IE8 | IE8 兼容性视图 | IE9 | IE9 兼容性视图 |
---|---|---|---|---|---|---|
documentMode | undefined | undefined | 8 | 7 | 9 | 7 |
在使用开发人员工具手动修改“文档模式”时,documentMode 的值也会跟着改变。
这里是小叉试验场的简版,请到正式版参与评论
锦灯笼混球