The logic of judgment is: the client is either Android, iOS, or PC. In fact, there are BlackBerry, SymbianOS, Windows Phone, etc. If necessary, you can add them by yourself. I have written in the third article.

Check Android or iOS with navigator.userAgent in your browser:

1. Determine whether it is Android or iOS

let u = navigator.userAgent;
let isAndroid = u.indexOf('Android') > - 1 || u.indexOf('Adr') > - 1;   // Check whether it is an Android terminal
letisIOS = !! u.match(/\(i[^;] +; ( U;) ? CPU.+Mac OS X/);     // Check whether it is an iOS terminal
console.log('Whether Android:', isAndroid); //true,false
console.log('iOS:', isIOS)
Copy the code
  • Encapsulation into methods:
  /* Determine the client */
  judgeClient() {
    let u = navigator.userAgent;
    let isAndroid = u.indexOf('Android') > - 1 || u.indexOf('Adr') > - 1;   // Check whether it is an Android terminal
    letisIOS = !! u.match(/\(i[^;] +; ( U;) ? CPU.+Mac OS X/);     // Check whether it is an iOS terminal
    console.log('Whether Android:' + isAndroid); //true,false
    console.log('iOS:' + isIOS);
    if(isAndroid){
      return 'Android';
    }else if(isIOS){
      return 'IOS';
    }else{
      return 'PC'; }},Copy the code

Determine whether it is Android or iOS

if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
  console.log('isIOS');
} else if (/(Android)/i.test(navigator.userAgent)) {
  console.log('isAndroid');
} else {
  console.log('isPC');
}
Copy the code
  • Encapsulation into methods:
  /* Determine the client */
  judgeClient() {
    let client = ' ';
    if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {  / / determine the iPhone | the | | iPod iOS
      client = 'iOS';
    } else if (/(Android)/i.test(navigator.userAgent)) {  / / determine the Android
      client = 'Android';
    } else {
      client = 'PC';
    }
    return client;
  },
Copy the code

3. Determine the PC or mobile terminal

  isPC() {
    /* PC if true, mobile if false
    let u = navigator.userAgent;
    let Agents = ["Android"."iPhone"."webOS"."BlackBerry"."SymbianOS"."Windows Phone"."iPad"."iPod"];
    let flag = true;
    for (let i = 0; i < Agents.length; i++) {
      if (u.indexOf(Agents[i]) > 0) {
        flag = false;
        break; }}return flag;
  },
Copy the code

Four. Identify multiple access terminals

  // Determine the access terminal
  let browser = {
    versions: function () {
      let u = navigator.userAgent, app = navigator.appVersion;
      return {
        trident: u.indexOf('Trident') > - 1./ / IE kernel
        presto: u.indexOf('Presto') > - 1./ / opera kernel
        webKit: u.indexOf('AppleWebKit') > - 1.// Apple, Google kernel
        gecko: u.indexOf('Gecko') > - 1 && u.indexOf('KHTML') = =- 1.// The Firefox kernelmobile: !! u.match(/AppleWebKit.*Mobile.*/), // Whether it is a mobile terminalios: !! u.match(/\(i[^;] +; ( U;) ? CPU.+Mac OS X/), / / ios terminal
        android: u.indexOf('Android') > - 1 || u.indexOf('Adr') > - 1./ / android terminal
        iPhone: u.indexOf('iPhone') > - 1.// Whether the browser is iPhone or QQHD
        iPad: u.indexOf('iPad') > - 1./ / whether the device
        webApp: u.indexOf('Safari') = =- 1.// Whether the web should be a program, without a header and a bottom
        weixin: u.indexOf('MicroMessenger') > - 1.// Whether wechat is available
        qq: u.match(/\sQQ/i) = =" qq" / / whether the QQ}; } (),language: (navigator.browserLanguage || navigator.language).toLowerCase()
  };
Copy the code
  • use
if (browser.versions.mobile) {
    console.log("is mobile");
}
if (browser.versions.ios) {
    console.log("is ios");
}
Copy the code

Determine the language the browser is currently using

  judgeLanguage() {
    // Determine the language the browser is currently using
    let currentLanguage = (navigator.browserLanguage || navigator.language).toLowerCase();    / / not IE
    if(! currentLanguage) {// Internet Explorer
      currentLanguage = navigator.browserLanguage;
    }
    console.log(currentLanguage);
    return currentLanguage;
  },
Copy the code

Micro channel small program “emoticon homemade artifact”, including huge scale love package (panda head, mushroom etc.) original picture material, easy to make emoticon package, page high appearance level, rich functions, welcome to visit ~