Skip to content
Related Articles

Related Articles

Improve Article

HTML | DOM onopen Event

  • Last Updated : 30 Jul, 2019
Geek Week

The DOM onopen event occurs on an open connection with an event source.

Related events:

  • onmessage: Received the message.
  • onerror: The problem occurs.

Syntax:

  • Using JavaScript:
    object.onopen = function(){myScript};
  • Using the addEventListener() method:
    object.addEventListener("open", myScript); 

Example: Using the addEventListener() method




<!DOCTYPE html>
<html>
  
<head>
    <title>
        HTML DOM onopen Event
    </title>
</head>
  
<body>
  
    <h1 id="gfg"></h1>
    <div id="geeks"></div>
  
    <script>
        if (typeof(EventSource) !== "undefined") {
            var source = new EventSource("/html/demo_sse.php");
            source.addEventListener("open", function() {
                document.getElementById("gfg").innerHTML = 
                  "GeeksforGeeks";
            });
  
            source.addEventListener("message", function(event) {
                document.getElementById("geeks").innerHTML +=
                  event.data + "<br>";
            });
  
        } else {
            document.getElementById("gfg").innerHTML = 
              "Browser does not support";
        }
    </script>
  
</body>
  
</html>

Output:

Supported Browsers: The browsers supported by HTML DOM onopen Event are listed below:

  • Google Chrome 9.0
  • Firefox 6.0
  • Apple Safari 5.0
  • Opera 11.0

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :