-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
75 lines (50 loc) · 2.58 KB
/
index.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
---
title: "XRSystem: devicechange event"
short-title: devicechange
slug: Web/API/XRSystem/devicechange_event
page-type: web-api-event
status:
- experimental
browser-compat: api.XRSystem.devicechange_event
---
{{APIRef("WebXR Device API")}}{{SeeCompatTable}}{{SecureContext_Header}}
A **`devicechange`** event is fired on an {{DOMxRef("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 {{DOMxRef("Event")}} with no added properties.
> **Note:** Not to be confused with the {{domxref("MediaDevices")}} {{DOMxRef("MediaDevices.devicechange_event", "devicechange")}} event.
## Syntax
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
```js
addEventListener("devicechange", (event) => {});
ondevicechange = (event) => {};
```
If the use of WebXR has been blocked by an `xr-spatial-tracking` [Permissions Policy](/en-US/docs/Web/HTTP/Permissions_Policy), `devicechange` events will not fire.
## Event type
A generic {{DOMxRef("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 {{domxref("XRSystem.isSessionSupported", "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:
```js
if (navigator.xr) {
navigator.xr.ondevicechange = (event) => {
// …
};
}
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}