JS get client system version

How to use code to get the browser client system and version.

// 判断是否是 android终端
let u = navigator.userAgent; let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; 

// 判断是否是 iOS终端
let isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); 

// true,false 
console.log('是否是Android:', isAndroid); 
console.log('是否是iOS:', isIOS)

Two function to use.

judgeClient() {
  let u = navigator.userAgent;
  let isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //判断是否是 android终端
  let isIOS = !!u.match(/(i[^;]+;( U;)? CPU.+Mac OS X/); //判断是否是 iOS终端
  console.log('是否是Android:' + isAndroid); //true,false
  console.log('是否是iOS:' + isIOS);
  if(isAndroid){
    return 'Android';
  }else if(isIOS){
    return 'IOS';
  }else{
  return 'PC';
}
}
judgeClient() {
let client = '';
if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) { //判断iPhone|iPad|iPod|iOS
client = 'iOS';
} else if (/(Android)/i.test(navigator.userAgent)) { //判断Android
client = 'Android';
} else {
client = 'PC';
}
return client;
}

Leave a Comment

Your email address will not be published. Required fields are marked *