-
Notifications
You must be signed in to change notification settings - Fork 22.4k
/
index.md
73 lines (47 loc) · 2.01 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
---
title: "SerialPort: connect event"
short-title: connect
slug: Web/API/SerialPort/connect_event
page-type: web-api-event
status:
- experimental
browser-compat: api.SerialPort.connect_event
---
{{APIRef("Web Serial API")}}{{SecureContext_Header}}{{SeeCompatTable}}{{AvailableInWorkers("window_and_dedicated")}}
The **`connect`** event of the {{domxref("SerialPort")}} interface is fired when a port has connected to the device. This event is only fired for ports associated with removable devices such as those connected via USB.
This event bubbles to the instance of {{domxref("Serial")}} that returned this interface.
## Syntax
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
```js
addEventListener("connect", (event) => {});
onconnect = (event) => {};
```
## Event type
A generic {{domxref("Event")}}.
## Bubbling
This event bubbles to {{domxref("Serial")}}. The `event.target` property refers to the {{domxref('SerialPort')}} object that bubbles up.
For more information, see [Event bubbling](/en-US/docs/Learn/JavaScript/Building_blocks/Event_bubbling).
## Examples
### Notify when a specific port connects
The {{domxref("Serial.requestPort()")}} method returns a {{jsxref("Promise")}} that resolves with a {{domxref("SerialPort")}} chosen by the user.
```js
// Prompt user to choose a serial port
const port = await navigator.serial.requestPort();
port.addEventListener("connect", (event) => {
// notify that the chosen port is connected
});
```
### Listening for any newly-connected ports
The `connect` event bubbles up to the {{domxref("Serial")}} object where you can listen for any newly-connected ports.
```js
navigator.serial.addEventListener("connect", (event) => {
// notify that a new port is available
// use `event.target` to refer to the newly-added port
});
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{domxref("SerialPort.disconnect_event", "disconnect")}} event