EventSource: sự kiện open

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since January 2020.

Note: This feature is available in Web Workers.

Sự kiện open của giao diện EventSource được kích hoạt khi kết nối với một nguồn sự kiện đã được mở.

Sự kiện này không thể hủy và không nổi bọt.

Cú pháp

Dùng tên sự kiện trong các phương thức như addEventListener(), hoặc đặt thuộc tính xử lý sự kiện.

js
addEventListener("open", (event) => { })

onopen = (event) => { }

Kiểu sự kiện

Một Event chung.

Ví dụ

js
const evtSource = new EventSource("sse.php");

// addEventListener version
evtSource.addEventListener("open", (e) => {
  console.log("The connection has been established.");
});

// onopen version
evtSource.onopen = (e) => {
  console.log("The connection has been established.");
};

Thông số kỹ thuật

Specification
HTML
# event-open
HTML
# handler-eventsource-onopen

Tương thích trình duyệt

Xem thêm