1. 程式人生 > 實用技巧 >JS 識別安卓還是ios蘋果、識別是否微信內建瀏覽器、手機型別

JS 識別安卓還是ios蘋果、識別是否微信內建瀏覽器、手機型別

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|SonyEricss
on|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)

東莞vi設計https://www.houdianzi.com/dgvi/ 豌豆資源網站大全https://55wd.com

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());