js获取浏览器对象的信息

js中有一个对象叫 navigator,navigator 对象包含有关浏览器的信息。所有的浏览器都支持该对象。
其中经常用到的是 navigator.userAgent 属性,通常,它是在 navigator.appCodeName(浏览器的名称) 的值之后加上斜线和 navigator.appVersion 的值构成的。
 
判断是PC还是手机浏览器:
第一种:
function IsPC() {
  var userAgentInfo = navigator.userAgent;
  var Agents = ["Android", "iPhone","SymbianOS", "Windows Phone", "iPad", "iPod"];
  var flag = true;
  for (var v = 0; v < Agents.length; v++) {
    if (userAgentInfo.indexOf(Agents[v]) > 0) {
      flag = false;
      break;
    }
  }
  return flag;
}

 

第二种:
function browserRedirect() {
  var sUserAgent = navigator.userAgent.toLowerCase();
  var bIsIpad = sUserAgent.match(/ipad/i) == "ipad";
  var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
  var bIsMidp = sUserAgent.match(/midp/i) == "midp";
  var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
  var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
  var bIsAndroid = sUserAgent.match(/android/i) == "android";
  var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
  var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";
  if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) ){
    window.location.href=B页面;
  }
}
 
 
判断是安卓还是iOS
 
常用:
function ismobile(test){
  var u = navigator.userAgent, app = navigator.appVersion;
  if(/AppleWebKit.*Mobile/i.test(navigator.userAgent) || (/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(navigator.userAgent))){
    if(window.location.href.indexOf("?mobile")<0){
      try{
        if(/iPhone|mac|iPod|iPad/i.test(navigator.userAgent)){
          return '0';
        }else{
          return '1';
        }
      }catch(e){}
      }
    }else if( u.indexOf('iPad') > -1){
      return '0';
    }else{
      return '1';
    }
};

 

posted @ 2018-01-21 20:50  狂奔的蜗牛1031  阅读(1602)  评论(0编辑  收藏  举报