如何检测移动设备和平板电脑分离的所有手持设备?

时间:2013-08-15 09:54:29

标签: javascript android jquery mobile tablet

我现在可以检测到所有手持设备但无法分离平板电脑和移动设备检测。我搜索过很多来源和q& a's,但是找不到解决方案。

自从jQuery 1.9.1移除$.browser方法以来。我们必须使用原生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

1 个答案:

答案 0 :(得分: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
   }
}
相关问题