根据ua检测ie浏览器版本

function IEMode(){
  var ua = navigator.userAgent.toLowerCase();
  var re_trident = /\btrident\/([0-9.]+)/;
  var re_msie = /\b(?:msie |ie |trident\/[0-9].*rv[ :])([0-9.]+)/;
  var version;

  if(!re_msie.test(ua)){return false;}

  var m = re_trident.exec(ua);
  if(m){
    version = m[1].split(".");
    version[0] = parseInt(version[0], 10) + 4;
    version = version.join(".");
  }else{
    m = re_msie.exec(ua);
    version = m[1];
  }
  return parseFloat(version);
}


var ie = IEMode();
if(ie && ie < 8){
  // 你的浏览器太老了。
}

var isIE6 = navigator.userAgent.indexOf("MSIE 6.0") !== -1;
var isIE7 = IEMode() == 7;
var isIE8 = navigator.userAgent.indexOf("Trident 4.0") !== -1;
var isIE9 = navigator.userAgent.indexOf("Trident 5.0") !== -1;
var isIE10 = navigator.userAgent.indexOf("Trident 6.0") !== -1;
var isIE11 = navigator.userAgent.indexOf("Trident 7.0") !== -1;

IEMode的作用是避免将IE内核的浏览器识别为IE,如搜狗浏览器、360浏览器等双核浏览器。

如果只需要简单的做下判断,可以用如下代码:

// 单个浏览器识别:
var isIE6 = navigator.userAgent.indexOf("MSIE 6.0") !== -1;
var isIE7 = navigator.userAgent.indexOf("MSIE 7.0") !== -1;
var isIE8 = navigator.userAgent.indexOf("MSIE 8.0") !== -1;
var isIE9 = navigator.userAgent.indexOf("MSIE 9.0") !== -1;
var isIE10 = navigator.userAgent.indexOf("MSIE 10.0") !== -1;
//!var isIE11 = navigator.userAgent.indexOf("IE 11.0") !== -1;
var isIE11 = /\btrident\/[0-9].*rv[ :]11\.0/.test(navigator.userAgent);

// 多个浏览器识别
var isIE67 = /\bMSIE [67]\.0\b/.test(navigator.userAgent);
var isIE678 = /\bMSIE [678]\.0\b/.test(navigator.userAgent);

发表评论

电子邮件地址不会被公开。 必填项已用*标注