问题描述
使用userAgent.match检测触控设备(智能手机和平板电脑)与台式机浏览器的代码是什么,并返回一个布尔变量(例如'isipad')?
What is the code to detect touch devices (smartphones and tablets) vs desktops browsers using userAgent.match and return a boolean variable (for example 'isipad')?
我需要主要针对Android和Apple设备进行测试。如果设备浏览器是Android或Apple,则返回isipad = false。否则,返回isipad = true。
I need to test this against Android and Apple devices mainly. If the device browser is Android or Apple, return isipad = false. Else, return isipad = true.
到目前为止,我这样做了(对于iDevice浏览器检测):
So far I went about it like this (for iDevice browser detection) :
if( navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) ||navigator.userAgent.match(/iPad/i) )
{var isipad = true;}
else
{var isipad = false;}
它似乎有效,但我希望能够添加Android浏览器,在这种情况下。
It seems to work, however I would love to be able to add the android browser as well, in this case.
提前致谢。
Alex
Thanks in advance.Alex
推荐答案
if ("ontouchstart" in document.documentElement)
{
alert("It's a touch screen device.");
}
else
{
alert("Others devices");
}
我在这里和那里浏览了大量的简单代码
most simple code i found after browsing a lot here and there
这篇关于如何使用javascript检测触摸设备浏览器与桌面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!