检测浏览器版本.js

function detectIE() {
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf('MSIE ');
    var trident = ua.indexOf('Trident/');

    if (msie > 0) { // IE 10 or older
        var ieVersion = parseInt(ua.substring(msie + 5, ua.indexOf('.', msie)), 10);
        console.log('当前浏览器版本:IE ' + ieVersion + ' detected');
        return ieVersion;
    }
    if (trident > 0) { // IE 11
        var rv = ua.indexOf('rv:');
        var ieVersion = parseInt(ua.substring(rv + 3, ua.indexOf('.', rv)), 10);
        console.log('当前浏览器版本:IE ' + ieVersion + ' detected');
        return ieVersion;
    }
    console.log('检测到非IE浏览器\  Non-IE browser detected');
    return false;
}

var ieVersion = detectIE();

if (ieVersion === 6) {
    console.log('Redirecting IE6 users to a special page...');
    window.location.href = 'ie6专属页面.html';
}
function getExplore(){
  var Sys = {};  
  var ua = navigator.userAgent.toLowerCase();  
  var s;  
  (s = ua.match(/rv:([\d.]+)\) like gecko/)) ? Sys.ie = s[1] :
  (s = ua.match(/msie ([\d\.]+)/)) ? Sys.ie = s[1] :  
  (s = ua.match(/edge\/([\d\.]+)/)) ? Sys.edge = s[1] :
  (s = ua.match(/firefox\/([\d\.]+)/)) ? Sys.firefox = s[1] :  
  (s = ua.match(/(?:opera|opr).([\d\.]+)/)) ? Sys.opera = s[1] :  
  (s = ua.match(/chrome\/([\d\.]+)/)) ? Sys.chrome = s[1] :  
  (s = ua.match(/version\/([\d\.]+).*safari/)) ? Sys.safari = s[1] : 0;  
  // 根据关系进行判断
  if (Sys.ie) return ('IE: ' + Sys.ie);  
  if (Sys.edge) return ('EDGE: ' + Sys.edge);
  if (Sys.firefox) return ('Firefox: ' + Sys.firefox);  
  if (Sys.chrome) return ('Chrome: ' + Sys.chrome);  
  if (Sys.opera) return ('Opera: ' + Sys.opera);  
  if (Sys.safari) return ('Safari: ' + Sys.safari);
  return 'Unkonwn';
}
var getExploreVersion = getExplore()
console.log('当前浏览器版本:'+getExploreVersion);

 

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注