我在计算机上的虚拟机上安装了 fedora,并且当我在手机上查看该网站时,我一直在尝试触发 deviceOrientation 事件。
它不起作用,因此使用此线程的答案:How to check for a device/browser that fully supports the deviceorientation event? ,我运行此代码来检查它是否受支持:
if (window.DeviceOrientationEvent && 'ontouchstart' in window) {
// setup real compass thing, with event.alpha
document.write("haz!");
} else {
// setup some mouse following hack
document.write("nope");
}
这会导致“不”。但我知道我的手机支持 deviceOrientation,因为 MDN 上的示例有效。
然后,我将完全相同文件上传到我自己的网站,你瞧,它给了我“haz!”。
我不知道为什么我使用的服务器会对是否支持 deviceOrientation 产生影响。我唯一能想到的是,也许它需要 SSL,但我在任何文档中都没有看到 SSL 提及,而且我之前在我的树莓派上托管的网站上使用过 deviceOrientation,该网站没有 SSL。
最佳答案
从 v.76 开始,Chrome 要求您所在的页面通过 https
提供服务。
这可以通过访问这个http://
fiddle来看到它将输出 nope
,而同样的 fiddle served through https://
将输出 haz!
,就像下面的代码片段一样,通过 https:
if (window.DeviceOrientationEvent) {
console.log("haz!");
} else {
console.log("nope");
}
Here is the chromium bug引入了此限制。
请注意,如果它仅用于您自己的目的,那么您可以通过使用 --unsafely-treat-insecure-origin-as-secure
启动 Chrome 来利用它。 ,显然it's also possible to do it on mobile versions (虽然我没有测试自己)。
关于javascript - DeviceOrientationEvent 不适用于所有服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57797232/