欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

JS 识别安卓还是ios苹果、识别是否微信内置浏览器、手机类型

程序员文章站 2022-05-01 20:30:54
1.识别手机平台是IOS还是安卓 /** * [isMobile 判断平台] * @param test: 0:iPhone 1:Android */ function ismobile(test) { var u = navigator.userAgent, app = navigator.app ......

1.识别手机平台是ios还是安卓

/**
* [ismobile 判断平台]
* @param test: 0:iphone    1:android
*/
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) { 
  //            alert(e);
      }
    }
  } else if (app.indexof('ipad') > -1|| app.indexof('iphone') > -1) {
     return "0:苹果";
  } else {
    return "1:安桌";
         
  }
};
ismobile(1)
 

 

2.识别是否微信内置浏览器

function isweixin() {
    var ua = window.navigator.useragent.tolowercase();
//          console.log(ua);//mozilla/5.0 (iphone; cpu iphone os 9_1 like mac os x) applewebkit/601.1.46 (khtml, like gecko)version/9.0 mobile/13b143 safari/601.1
    if (ua.match(/micromessenger/i) == 'micromessenger') {
        return true;
    } else {
        return false;
    }
}

3.识别移动终端手机品牌

function judgebrand(suseragent) {
//      var isiphone = suseragent.match(/iphone/i) == "iphone";
  var ishuawei = suseragent.match(/huawei/i) == "huawei";
  var ishonor = suseragent.match(/honor/i) == "honor";
  var isoppo = suseragent.match(/oppo/i) == "oppo";
  var isoppor15 = suseragent.match(/pacm00/i) == "pacm00";
  var isvivo = suseragent.match(/vivo/i) == "vivo";
  var isxiaomi = suseragent.match(/mi\s/i) == "mi ";
  var isxiaomi2s = suseragent.match(/mix\s/i) == "mix ";
  var isredmi = suseragent.match(/redmi/i) == "redmi";
  var issamsung = suseragent.match(/sm-/i) == "sm-";
  var ismeizu = suseragent.match(/meizu/i) == "meizu";
​
//      if (isiphone) {
//        return 'iphone';
//      } else
  
  if (ishuawei || ishonor) {
    return 'huawei';
  } else if (isoppo || isoppor15) {
    return 'oppo';
  } else if (isvivo) {
    return 'vivo';
  } else if (isxiaomi || isredmi || isxiaomi2s) {
    return 'xiaomi';
  } else if (issamsung) {
    return 'samsung';
  } else if (ismeizu) {
    return 'meizu';
  } else {
    return '';
  }
}
var brand = judgebrand(navigator.useragent.tolowercase());