Javascript 如何检查完全支持deviceorientation事件的设备/浏览器?

Javascript 如何检查完全支持deviceorientation事件的设备/浏览器?,javascript,html,mobile,magnetometer,Javascript,Html,Mobile,Magnetometer,我有一个HTML5指南针,它工作得很好。现在,我需要一种聪明的方法来区分“伪设备定向”浏览器(如桌面chrome和FF)和真正的候选浏览器,如使用设备磁强计的iPhone/Android/iPad浏览器 我当前的解决方案是对DeviceOrientationEvent和touch支持进行基本检查: if (window.DeviceOrientationEvent && 'ontouchstart' in window) { // setup real compass thi

我有一个HTML5指南针,它工作得很好。现在,我需要一种聪明的方法来区分“伪设备定向”浏览器(如桌面chrome和FF)和真正的候选浏览器,如使用设备磁强计的iPhone/Android/iPad浏览器

我当前的解决方案是对DeviceOrientationEvent和touch支持进行基本检查:

if (window.DeviceOrientationEvent && 'ontouchstart' in window) {
  // setup real compass thing, with event.alpha
} else {
  // setup some mouse following hack
}

够了吗?我真的不确定“支持触摸但没有磁强计”的设备,比如mac笔记本上的chrome有触摸支持,还有一个假的deviceorientation

它似乎按原样工作,我错了,mac book上的chrome没有进入“真正的指南针”部分。也许应该测试一下

if (window.DeviceOrientationEvent && 'ontouchstart' in window) {
    // setup real compass thing, with event.alpha
    document.body.innerHTML = "haz!";
} else {
    // setup some mouse following hack
    document.body.innerHTML = "nope";
}
检查自己:


如果一台支持触摸功能的windos笔记本电脑上显示了支持deviceorientation的chrome浏览器,则会发生什么情况。可能会检查deviceorientation事件的“绝对”属性:

if(window.innerHeight>window.innerWidth){alert(“横向定位!”);}也可以参考函数is_touch_device(){try{document.createEvent(“TouchEvent”);return true;}catch(e)测试触摸支持{return false;}}}thx,但它不是关于方向的改变。它是关于“它内置了真正的指南针吗?”。请参阅我的chrome on mac book中底部的示例……ontouchstart与设备定向有何关系?请尝试以某种方式检查设备是否内置了真正的指南针。并可能使用Modernizer,以实现更明智的触摸功能检测。