JavaScript 判断 IE6/IE7/IE8/IE9

返回列表

代码日记 | 小叉 | 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。具体变化可以参考下表:

浏览器

IE6IE7IE8IE8 兼容性视图IE9IE9 兼容性视图
documentModeundefinedundefined8797

  在使用开发人员工具手动修改“文档模式”时,documentMode 的值也会跟着改变。

标签
documentMode
参考资料
1documentMode property (Internet Explorer)
  1. 竟然有IE6!FUCK IE6!
    2012-04-27 12:47:06
  2. 游客
    我的IE8,ieMode居然弹出来一个 5
    2012-06-15 21:58:16
  3. 你看看页面有没有写 DOCTYPE
    触发 IE 的 Quirks Mode 时,documentMode 的值会是 5
    2012-06-17 19:42:13
  4. 学习了
    2015-10-13 13:55:25

这里是小叉试验场的简版,请到正式版参与评论

下一篇:教你怎么把图标字体(IconFont)转为 PNG

上一篇:PHP 判断是否为 AJAX 请求

返回列表