如何用userAgent识别android和linux用户

时间:2013-06-26 10:03:50

标签: javascript android linux

在我的桌面网站上,我获得了此代码,可将移动用户重定向到我的网站的移动版本:

<script type="text/javascript">
<!--
if ((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)))
{
   location.replace("http://YOUR-MOBILE-SITE.com");
}
-->
</script>

这适用于iOS但我想要像

这样的东西
else if (navigator.userAgent.match(/Android/i))
{
    location.replace...
}
else if (navigator.userAgent.match(/blackberry/i))
{
    location...
}

应该是什么,而不是/ android /和/ blackberry /?

如何用linux做到这一点? / LINUX /?

2 个答案:

答案 0 :(得分:0)

使用媒体查询定位特定宽度,但不定位设备。 您有运行Android的电视,平板电脑,智能手机,具有不同分辨率和屏幕尺寸的半智能手机。您也可以在Android上运行任意数量的浏览器,例如firefox。

@media (max-width:320px){
///codezz
}
@media (max-width:1024px){
///codezz
}

等等'

如果您坚持使用用户代理进行此操作,请参阅此链接以获取签名: https://developers.google.com/chrome/mobile/docs/user-agent

    if(navigator.userAgent.indexOf('Android') > 0){
    //    runcodez
   };

答案 1 :(得分:0)

    else if (navigator.userAgent.indexof("Android")!=-1)
    {
        location.replace...
    }
    //User Agent in BlackBerry 6 and BlackBerry 7/BlackBerry Device Software 4.2 to 5.0
    else if (navigator.userAgent.indexof("BlackBerry") ! = -1)
    {
        location...
    }
    //User Agent in BlackBerry Tablet OS
    else if (navigator.userAgent.indexof("PlayBook") ! = -1)
    {
        location...
    }
    //User Agent in BlackBerry 10
    else if (navigator.userAgent.indexof("BB10") ! = -1)
    {
        location...
    }
else if (navigator.userAgent.indexof("linux") ! = -1)
    {
        location...
    }