JS-环境判断:小程序、公众号、浏览器、APP、安卓、IOS

介绍

本文主要介绍获取当前页面所在的环境,区分微信小程序、微信公众号H5、浏览器、app环境,终端判断是否是IOS、是否是Android~

详情参考:https://timor419.github.io/2021/11/25/JS-getEnvironment/#more

JS代码:

const ua = navigator.userAgent.toLowerCase();

微信小程序、微信公众号H5、浏览器、app环境

getENVIR: () => {
    
    
  const isWeixin = ua.indexOf('micromessenger') !== -1;
  const isInApp = /(^|;\s)app\//.test(ua);
  if (isWeixin) {
    
    
    if ((window as any).__wxjs_environment === 'miniprogram') {
    
    
      return 'wxapp';
    } else {
    
    
      return 'wxh5';
    }
  } else {
    
    
    if (!isInApp) {
    
    
      return 'browser';
    } else {
    
    
      return 'app';
    }
  }
},

终端判断:是否是IOS

checkIfIOS: () => {
    
    
  return ua.match(/(iphone|ipod|ipad);?/i);
},

终端判断:是否是Android

checkIfAndroid: () => {
    
    
  return ua.match(/android|adr/i);
},

------------- The End -------------

许可协议: 转载请保留原文链接及作者。

猜你喜欢

转载自blog.csdn.net/weixin_43937466/article/details/121539118