javascript - DeviceOrientationEvent 不适用于所有服务器

标签 javascript server device-orientation

我在计算机上的虚拟机上安装了 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/

相关文章:

ios - 处理具有两个不同 View 的 View Controller 的最佳方法: portrait and landscape

javascript - 一次只在搜索栏中显示 5 个结果

javascript - 将数字转换为完整的二进制八位字节

javascript - 使用 JavaScript 和 jQuery 创建便签

javascript - 从图像创建自己的 map 并添加图钉

database - QGIS服务器可以从数据库读取项目吗

eclipse - 如何在没有IDE的情况下在Tomcat上部署Restful

android - 当方向改变时使用选项卡重新加载 Activity 时, fragment 被初始化两次

google-maps-api-3 - 谷歌地图js API V3 : show orientation arrow on "my location marker"

java - Socket和ServerSocket通信不清楚