首页 > 技术文章 > H5 环境检测

NorthHarbor 2020-03-18 09:41 原文

  • 检测是否在客户端App内
function is_app() {
    var userAgent = navigator.userAgent.toLowerCase();//获取UA信息
    if (userAgent.indexOf("xxxxxx") != -1) {    //判断ua中是否含有和app端约定好的标识xxxxxx
        return true  // 在app内
    } else {
        return false  // 不在 app 内
    }
}
  • 检测是否在Android内
function isAndroid() {
    const userAgent = navigator.userAgent;
    return userAgent.indexOf('Android') > 0;
},
  • 检测是否在IOS内
function isAndroid() {
    const userAgent = navigator.userAgent;
    return /(iPhone|iPad|iPod)/i.test(userAgent);
},

推荐阅读