【JavaScript】移动端扫描二维码检测浏览器(微信、支付宝、Safari、其他浏览器)

// 检测客户端类型
// 注意:19年6月份iPad出了专门的操作系统iPadOS
// 它的userAgent中不是iPad,也没有下文中的mobile,而是Macintosh,类似Mac。需要兼容的可以加上。
function judgeClient() {
  var client = "";
  if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
    client = "ios";
  } else if (/(Android)/i.test(navigator.userAgent)) {
    client = "android";
  } else {
    client = "pc";
  }
  return client;
}

// 检测浏览器(微信、支付宝、Safari、其他浏览器)
function whichBrowser(){

    var versionFileUrl = document.getElementById("downloadUrl").innerText;

    var clientType = client;

    var browser = navigator.userAgent.toLowerCase();

    var IOSTip = "点击右上角按钮,然后在弹出的菜单中,点击Safari中打开,即可安装";

    var IOS4Other = "该浏览器不支持安装App。请在Safari中打开,即可安装";

    var ANDROIDTip = "点击右上角按钮,然后在弹出的菜单中,点击在浏览器中打开,即可安装";

    if(/Alipay/i.test(browser) || /MicroMessenger/i.test(browser)){

        if(clientType === "ios"){
            alert(IOSTip);
        }else if(clientType === "android"){
            alert(ANDROIDTip);
        }
    }else if(browser.indexOf('applewebkit') > -1 && browser.indexOf('mobile') > -1 && browser.indexOf('safari') > -1 &&
        browser.indexOf('linux') === -1 && browser.indexOf('android') === -1 && browser.indexOf('chrome') === -1 &&
        browser.indexOf('ios') === -1 && browser.indexOf('browser') === -1){

        window.location.href = "itms-services://?action=download-manifest&url=" + versionFileUrl;
    }else {

        if(clientType === "ios"){
            alert(IOS4Other);
        }else if(clientType === "android"){
            window.location.href = versionFileUrl;
        }
    }
}
发布了121 篇原创文章 · 获赞 116 · 访问量 39万+

猜你喜欢

转载自blog.csdn.net/Mr_EvanChen/article/details/102675950