Javascript 如何在桌面和移动Chrome用户代理之间进行检测?
对于Chrome桌面扩展主页,我试图检测用户是在Android上使用Chrome桌面版还是移动版。目前,下面的脚本识别Android Chrome与桌面Chrome相同。在桌面Chrome上应该显示“Chrome”链接;然而,如果有人使用Android的Chrome浏览器,它应该显示“mobile other”链接 脚本:Javascript 如何在桌面和移动Chrome用户代理之间进行检测?,javascript,google-chrome,browser-detection,Javascript,Google Chrome,Browser Detection,对于Chrome桌面扩展主页,我试图检测用户是在Android上使用Chrome桌面版还是移动版。目前,下面的脚本识别Android Chrome与桌面Chrome相同。在桌面Chrome上应该显示“Chrome”链接;然而,如果有人使用Android的Chrome浏览器,它应该显示“mobile other”链接 脚本: <script>$(document).ready(function(){ var ua = navigator.userAgent; if (/
<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>
$(文档).ready(函数(){
var ua=navigator.userAgent;
如果(/Chrome/i.test(ua))
$('a.chrome').show();
否则如果(/Android | webOS | iPhone | iPad | iPod |黑莓| IEMobile | Opera Mini | Mobile | Mobile/i.test(ua))
$('a.mobile-other').show();
其他的
$('a.desktop-other').show();
});
Chrome Android用户代理:
Mozilla/5.0 (Linux; <Android Version>; <Build Tag etc.>) AppleWebKit/<WebKit Rev> (KHTML, like Gecko) Chrome/<Chrome Rev> Mobile Safari/<WebKit Rev>
Mozilla/5.0(Linux;;)AppleWebKit/(KHTML,比如Gecko)Chrome/mobilesafari/
问题在于,无论是桌面版还是移动版,用户代理都将始终使用“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();
});
如果您将
else if(/Android |…
更改为if(/Android |…
),会发生什么情况?@imtheman显示两个按钮--“mobile other”和“chrome”按钮,然后交换第一个if
和else if
逻辑。当前的chrome手机版本在iOS上(v43.*)用户代理字符串中没有Chrome
。From:“Chrome for iOS中的UA与Mobile Safari用户代理相同,使用CriOS/而不是Version/”