【发布时间】:2014-10-13 04:05:19
【问题描述】:
对于 Chrome 桌面扩展主页,我正在尝试检测用户是在 Android 上使用 Chrome for Desktop 还是 Chrome for Mobile。目前,下面的脚本将 Android Chrome 识别为与桌面版 Chrome 相同。在桌面 Chrome 上,它应该显示“chrome”链接;但是,如果有人使用 Android 版 Chrome,它应该显示“mobile-other”链接。
脚本:
<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>
【问题讨论】:
-
如果您将
else if (/Android|...更改为if (/Android|...会怎样? -
@imtheman 显示两个按钮——“mobile-other”和“chrome button”
-
好的,然后交换第一个
if和else if逻辑。
标签: javascript google-chrome browser-detection