EventSource: error event
MDN Web Docs · نسخهٔ df622a2a1c1e · CC BY-SA 2.5 و مجوز جداگانهٔ ناشر برای نمونهکد
{{APIRef("Server Sent Events")}}{{AvailableInWorkers}}
The **error** event of the {{domxref("EventSource")}} API is fired when a connection with an event source fails to be opened.
This event is not cancelable and does not bubble.
Syntax
Use the event name in {{domxref("EventTarget/addEventListener", "addEventListener()")}}:
js-nolint
addEventListener("error", (event) => { })Alternatively, assign to the event handler property of the interface:
js-nolint
onerror = (event) => { }Event type
A generic {{domxref("Event")}}.
Examples
js
const evtSource = new EventSource("sse.php");
// addEventListener version
evtSource.addEventListener("error", (e) => {
console.log("An error occurred while attempting to connect.");
});
// onerror version (but note that this replaces other event listeners
// added using onerror, if any)
evtSource.onerror = (e) => {
console.log("An error occurred while attempting to connect.");
};Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
- Using server-sent events
- [
open](/en-US/docs/Web/API/EventSource/open_event) - [
message](/en-US/docs/Web/API/EventSource/message_event)