如何检测桌面版和移动Chrome用户代理之间的关系? [英] How do you detect between a Desktop and Mobile Chrome User Agent?

查看:172
本文介绍了如何检测桌面版和移动Chrome用户代理之间的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于Chrome桌面扩展程序主页,我试图检测用户是在Android上使用Chrome for Desktop还是Chrome for Mobile。目前,下面的脚本将Android Chrome与Desktop Chrome相同。在桌面版Chrome上,它应该显示chrome链接;但是,如果某人使用的是Android版Chrome,则应显示手机其他链接。

脚本:

 < script> $(document)。 ready(function(){
var ua = navigator.userAgent; $ b $ if(/Chrome/i.test.ua))
$('a.chrome')。show();
$ b $ else if(/ Android | webOS | iPhone | iPad | iPod | BlackBerry | IEMobile | Opera Mini | Mobile | mobile / i.test(ua))
$('a.mobile -other')。show();

else
$('a.desktop-other')。show();
});< / script>

Chrome Android用户代理:



<$ p $ Mozilla / 5.0(Linux;< Android版本>;< Build Tag等)> AppleWebKit /< WebKit Rev> (KHTML,如Gecko)Chrome /< Chrome Rev> Mobile Safari /< WebKit Rev>


解决方案

问题是用户代理将始终拥有Chrome 无论是桌面版还是手机版。所以你必须首先检查更具体的情况。

  $(document).ready(function(){
var ua = navigator.userAgent;
$ b如果(/ Android | webOS | iPhone | iPad | iPod | BlackBerry | IEMobile | Opera Mini | Mobile | mobile | CriOS / i.test(ua))
$('a.mobile-other')。show();

else if(/Chrome/i.test(ua))
$('a.chrome')) .show();

else
$('a.desktop-other')。show();
});


For a Chrome Desktop Extension home page, I'm trying to detect whether a user is using Chrome for Desktop or Chrome for Mobile on Android. Currently the script below identifies Android Chrome the same as Desktop chrome. On desktop Chrome it should show "chrome" link; however, if someone is on Chrome for Android, it should show the "mobile-other" link.

Script:

<script>$(document).ready(function(){
    var ua = navigator.userAgent;
    if (/Chrome/i.test(ua))
       $('a.chrome').show();

    else if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile/i.test(ua))
       $('a.mobile-other').show();

    else
       $('a.desktop-other').show();
  });</script>

Chrome Android User Agent:

Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev> (KHTML, like Gecko) Chrome/<Chrome Rev> Mobile Safari/<WebKit Rev>

解决方案

The problem is the user agent will always have "Chrome" whether it is the desktop or mobile version. So you have to check the more specific case first.

$(document).ready(function(){
    var ua = navigator.userAgent;

    if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile|CriOS/i.test(ua))
       $('a.mobile-other').show();

    else if(/Chrome/i.test(ua))
       $('a.chrome').show();

    else
       $('a.desktop-other').show();
});

这篇关于如何检测桌面版和移动Chrome用户代理之间的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆