onopen Event

Definition and Usage

The onopen event occurs when the connection with the event source is opened.

Related Events:

  • onmessage Occurred when a message is received
  • onerror Occurred when there is a problem

For more knowledge about server-sent events, please learn our HTML5 Server-Sent Events Tutorial.

Example

Create a new EventSource object and specify the URL of the page that sends updates.

When (and if) a connection is established, output some text in the <h1> element:

var source = new EventSource("demo_sse.php");
source.onopen = function() {
  document.getElementById("myH1").innerHTML = "Getting server updates";
};

Try it yourself

Syntax

object.onopen = function(){myScript};

Try it yourself

Using addEventListener() Method:

object.addEventListener("open", myScript);

Try it yourself

Note:Internet Explorer 8 and earlier versions do not support addEventListener() Method.

Technical Details

Bubbling: Not Supported
Cancellable: Not Supported
Event Type: Event

Browser Support

The numbers in the table indicate the first browser version that fully supports the event.

Events Chrome IE Firefox Safari Opera
onopen 9.0 Not Supported 6.0 5.0 11.0