【问题标题】:How to detect all handheld devices with separation of mobile and tablet?如何检测手机和平板分离的所有手持设备?
【发布时间】:2013-08-15 09:54:29
【问题描述】:

目前我可以检测所有手持设备,但无法将平板电脑和移动设备检测分开。我搜索了很多资源和问答,但找不到解决方案。

自从$.browser 方法从jQuery 1.9.1 中删除。我们必须用原生 js 来做。

Here is testing jsFiddle.

javascript:

/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ? isTabletMobile = true : isTabletMobile = false;
//this works perfect

//problem starts when i try to detect difference between mobile and tablet

/iPad/i.test(navigator.userAgent) ? isTablet = true : isTablet = false;
//can't detect other tablet devices

/Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ? isMobile = true : isMobile = false;
//can't exclude tablet devices from here (like Android tablet devices)

if ( isTabletMobile  ) {
    alert('You are on Mobile or Tablet');
}else{
    alert('You are on Destop device');
}

if ( isTablet ) {
    alert('You are on Tablet');
}
if ( isMobile ) {
    alert('You are on Mobile');
}

Source

【问题讨论】:

    标签: javascript android jquery mobile tablet


    【解决方案1】:

    您应该检查屏幕尺寸。

    尝试对平板电脑的最小尺寸进行一些研究。

    那么如果您正在检查的设备的大小大于或等于 到平板电脑的最小尺寸,这意味着该设备是平板电脑。

    var isDevice = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ? true : false;
    
    var tabletMinWidth,
        tabletMinHeight;
    
    if(isDevice){
       if(tabletMinWidth <= deviceWidth && tabletMinHeight <= deviceHeigth){
          isTablet = true
       }
    }
    

    【讨论】:

    • +1 以获得不错的替代解决方案,但这仍然不是可靠的方法。 tabletMinWidth 变量无法检测到,因为每天都有更高分辨率的新设备。 Here is device sizes list.
    猜你喜欢
    • 1970-01-01
    • 2012-07-05
    • 2018-05-10
    • 2023-03-12
    • 2012-10-11
    • 2020-12-21
    • 1970-01-01
    • 2012-03-20
    • 2015-05-28
    相关资源
    最近更新 更多