简体   繁体   English

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

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

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. 对于Chrome桌面扩展主页,我正在尝试检测用户是否在Android上使用Chrome for Desktop或Chrome for Mobile。 Currently the script below identifies Android Chrome the same as Desktop chrome. 目前,下面的脚本将Android Chrome与Desktop chrome相同。 On desktop Chrome it should show "chrome" link; 在桌面Chrome上,它应该显示“chrome”链接; however, if someone is on Chrome for Android, it should show the "mobile-other" link. 但是,如果有人在Chrome for Android上,它应该显示“移动其他”链接。

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

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. 问题是用户代理将始终拥有“Chrome”,无论是桌面版还是移动版。 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();
});

So to update @imtheman's code according to the newest Chrome for iOS user agent string: 因此,要根据最新的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.

相关问题 检测移动用户和桌面是否相同 - Detect if mobile user and desktop is same 如何检测 iOS Mobile Safari 和 Chrome 的“请求桌面站点”模式? - How to detect “request desktop site” mode of iOS Mobile Safari and Chrome? 如何检测移动设备或桌面设备而不被 chrome 开发工具“欺骗” - How to detect mobile or desktop and not get "cheated" by chrome dev tools 在桌面上为iframe设置移动用户代理 - Setting mobile user agent for iframe on desktop 如何检测用户使用的是手机,平板电脑还是台式机并重定向它们? - How to detect whether the user is using mobile, tablet or desktop and redirect them? 如何使用javascript检测移动设备? - How do you detect a mobile device with javascript? 我的“ isMMobile()”功能正在将桌面用户代理检测为移动 - My “isMMobile ()” function is detecting desktop user agent as mobile 如何检测用户使用的是 Excel 在线版还是 Excel 桌面版 - How do I detect if the user is using Excel online or Excel desktop 如何检测移动设备并获取用户代理信息只发送一次并将该信息保存到服务器上的数据库? - How to detect mobile device and get user agent info send and save that information to database on server, only once? 您如何检测Chrome中是否阻止了弹出到另一个域的弹出窗口? - How do you detect if a popup to another domain was blocked in Chrome?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM