HTML | DOM onmessage Event

The onmessage Event in HTML DOM used when an object has received some message through an event source.

The event object for onmessage Event supports the following schemes:

  • data: It contains the actual message present inside it.
  • origin: It contains the URL of the document that invoked the event.
  • lastEventId: It works as descriptive attribute for the last message that was spotted in the message stream.

Note: Server-Sent Events are not supported in Internet Explorer.



Syntax:

  • object.onmessage = function(){myScript}; 
  • Using the addEventListener() method
    object.addEventListener("message", myScript);

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Example 2: Using the addEventListener() method

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
   
<head>
    <title>
        HTML DOM onmessage Event
    </title>
</head>
   
<body>
    <h1 id="gfg"></h1>
    <div id="geeks"></div>
       
    <!--main Function-->
    <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("geeks").innerHTML = 
                             "Your browser does not supported";
        }
    </script>
   
</body>
   
</html>

chevron_right


Output:

Supported browsers: The browser supported by onmessage Event are listed below:

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


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.