[英]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.