JavaScript的如何检查用户代理手机/平板电脑平板、用户、电脑、手机

2023-09-06 08:03:02 作者:中单给点力

我目前正在开发一个客户的网站,其中有跨台式机和平板电脑平台不同的功能一些JS的工作。试想一下:

I'm currently developing some JS work for a clients website which has different functionality across desktop and tablet platforms. Consider:

if(! navigator.userAgent.match(/Android/i) &&
            ! navigator.userAgent.match(/webOS/i) &&
            ! navigator.userAgent.match(/iPhone/i) &&
            ! navigator.userAgent.match(/iPod/i) &&
            ! navigator.userAgent.match(/iPad/i) &&
            ! navigator.userAgent.match(/Blackberry/i) )
    {
        // do desktop stuff

    } else if ( navigator.userAgent.match(/iPad/i) ) 
    {
       // do tablet stuff

    }

目前,我只检查iPad作为检查机器人似乎有点问题,而且是一个非常宽泛的术语。是否有针对Android平板电脑与功放之间区分已知的方法;移动使用JS?

Currently, I'm only checking for iPad as checking for "android" seems somewhat problematic, and is a very broad term. Is there a known method for distinguishing between Android tablet & mobile using JS?

非常感谢, 迈尔斯

推荐答案

选中此页: http://www.quirksmode.org/js/detect.html

和这一个: http://detectmobilebrowsers.com/

我相信你会找到答案。 还要检查这个堆栈溢出职位 JavaScript.解决方法来检测手机浏览器

I am sure you will find your answer. Also check this stack overflow post JavaScript. Solution to detect mobile browser

希望它能帮助!