繁体   English   中英

如何检测桌面和移动Chrome用户代理?

[英]How do you detect between a Desktop and Mobile Chrome User Agent?

对于Chrome桌面扩展主页,我正在尝试检测用户是否在Android上使用Chrome for Desktop或Chrome for Mobile。 目前,下面的脚本将Android Chrome与Desktop chrome相同。 在桌面Chrome上,它应该显示“chrome”链接; 但是,如果有人在Chrome for Android上,它应该显示“移动其他”链接。

脚本:

<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用户代理:

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

问题是用户代理将始终拥有“Chrome”,无论是桌面版还是移动版。 所以你必须先检查更具体的案例。

$(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 for iOS用户代理字符串更新@ imtheman的代码:

$(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();
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM