GO-V.ORG
As seen in

XRSystem: devicechange event

WebXR Device API

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 devicechange event.

## Syntax

Use the event name in addEventListener():

```js-nolint
addEventListener("devicechange", (event) => { })
```

Alternatively, assign to the [event handler property](/docs/Web/API/Document_Object_Model/Events#using_onevent_properties) of the interface:

```js-nolint
ondevicechange = (event) => { }
```

If the use of WebXR has been blocked by an `xr-spatial-tracking` [Permissions Policy](/docs/Web/HTTP/Guides/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](#examples) 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.

```js
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 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):

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

## Specifications

## Browser compatibility

source: MDN Web Docs mirror · page

Cite this

"XRSystem: devicechange event". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/XRSystem/devicechange_event

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·