SerialPort: disconnect event

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.

The disconnect event of the SerialPort interface is fired when the port has disconnected from 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 Serial that returned this interface.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js

addEventListener("disconnect", (event) => {});

ondisconnect = (event) => {};

Event type

A generic Event.

Bubbling

This event bubbles to Serial. The event.target property refers to the SerialPort object that bubbles up.

For more information, see Event bubbling.

Examples

Notify when a specific port disconnects

Here the event listener is installed on a specific SerialPort object.

js

port.addEventListener("disconnect", (event) => {
  // notify that the port has become unavailable
});

Listening for any ports that become unavailable

The disconnect event bubbles up to the Serial object where you can listen for any ports that become unavailable.

js

navigator.serial.addEventListener("disconnect", (event) => {
  // notify that a port has become unavailable
  // use `event.target` to refer to the unavailable port
});

Specifications

Specification
Web Serial API
# dfn-disconnect
Web Serial API
# dom-serialport-ondisconnect

Browser compatibility

BCD tables only load in the browser

See also