Serial.ondisconnect

Secure context

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

The ondisconnect EventHandler of the Serial interface is called when the port has been disconnected from the device. This method receives an Event object. The target of this event is the SerialPort interface that has been disconnected.

This event is only fired for ports associated with removable devices such as those connected via USB. The user must grant the origin permission to access this device during a call to requestPort() before this event will be fired.

Syntax

Serial.ondisconnect = function(event);
Serial.addEventListener('disconnect', function(event));

Example

The following example shows an event listener for the disconnect event,. This allows the site to react when a port is disconnected.

navigator.serial.addEventListener('disconnect', (e) => {
  // Remove `e.target` from the list of available ports.
});

Specifications

Specification
Web Serial API
# dom-serial-ondisconnect

Browser compatibility

BCD tables only load in the browser