Serial.onconnect

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

The onconnect event handler of the Serial interface is called when a port has been connected from a 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.onconnect = function(event);
Serial.addEventListener('connect', function(event));

Example

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

navigator.serial.addEventListener('connect', (e) => {
  // Connect to `e.target` or add it to a list of available ports.
});

Specifications

Specification
Web Serial API
# dom-serial-onconnect

Browser compatibility

BCD tables only load in the browser