大网数据广告活动
Web 标准与前端开发

XRSystem: devicechange event

· 52CE 我爱测

A **`devicechange`** event is fired on an XRSystem object 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 Event with no added properties.

WebXR Device API

A devicechange event is fired on an XRSystem object 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 Event with no added properties.

[!NOTE] Not to be confused with the MediaDevices MediaDevices.devicechange_event event.

Syntax

Use the event name in EventTarget/addEventListener:

addEventListener("devicechange", (event) => { })

Alternatively, assign to the event handler property of the interface:

ondevicechange = (event) => { }

If the use of WebXR has been blocked by an xr-spatial-tracking Permissions Policy, devicechange events will not fire.

Event type

A generic Event with no added properties.

Description

Trigger

Triggered whenever the availability of immersive XR devices has changed. For example, when a VR headset or AR goggles have been connected or disconnected.

Use cases

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 example below.

Examples

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) => {
      enableXRButton.disabled = !immersiveOK;
    });
  });
}

When devicechange is received, the handler set up in this code calls the XR method XRSystem.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.

You can also use the ondevicechange event handler property to set a single handler for devicechange events (but note that this replaces other event listeners added using ondevicechange, if any):

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

Specifications

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。