onopen = myfunction(); - Javascript DOM Event

Javascript examples for DOM Event:Element Event Attribute

Description

The onopen event occurs when a connection with an event source is opened.

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<h1 id="myH1"></h1>
<div id="myDIV"></div>

<script>
if(typeof(EventSource) !== "undefined") {
    var source = new EventSource("/html/demo_sse.php");
    source.onopen = function() {//from www.ja v a 2  s .c  o m
        document.getElementById("myH1").innerHTML = "Getting server updates";
    };

    source.onmessage = function(event) {
        document.getElementById("myDIV").innerHTML += event.data + "<br>";
    };

} else {
    document.getElementById("myDIV").innerHTML = "Sorry, your browser does not support server-sent events...";
}
</script>

</body>
</html>

Related Tutorials