A devicechange event is fired on an XRSystem object whenever the whenever the availability of immersive XR devices has changed; for example, a VR headset or AR goggles have been connected or disconnected. It's a generic 事件 with no added properties.

注意: Not to be confused with the MediaDevices devicechange 事件。

冒泡 No
可取消 No
接口 事件
Event handler XRSystem.ondevicechange

用法注意事项

devicechange events are not delivered if the document which owns the XRSystem object has been granted permission to do so through the xr-spatial-tracking feature policy.

You can use this event to, for example, monitor for the availability of a WebXR-compatible device so that you can enable a UI element which the user can use to activate immersive mode. This is shown in the 范例 下文。

范例

The example shown here handles the devicechange event by toggling the availability of the "Enter XR" button based on whether or not any immersive devices are currently available.

if (navigator.xr) {
  navigator.xr.addEventListener("devicechange", event => {
    navigator.xr.isSessionSupported("immersive-vr")
    .then(immersiveOK) => {
      if (immersiveOK) {
        enableXRButton.disabled = false;
      } else {
        enableXRButton.disabled = true;
      }
    });
  });
}
					

devicechange is received, the handler set up in this code calls the XR 方法 isSessionSupported() to find out if there's a device available that can handle immersive VR presentations. If there is, the button to enter XR mode is enabled; otherwise it's disabled.

还可以使用 ondevicechange event handler property to set a single handler for devicechange 事件:

if (navigator.xr) {
  navigator.xr.ondevicechange = event => {
    /* ... etc ... */
  };
}
					

规范

规范 状态 注释
WebXR 设备 API
The definition of 'devicechange event' in that specification.
工作草案 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
devicechange event Chrome 79 Edge 79 Firefox No IE No Opera No Safari No WebView Android No Chrome Android 79 Firefox Android No Opera Android No Safari iOS No Samsung Internet Android 11.2

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

另请参阅

元数据

  • 最后修改: