How can I handle Server-Sent Events in HTML5?

JavascriptWeb DevelopmentFront End Scripts

To handle Server-Sent Events in HTML5, you can try to run the following code:

<!DOCTYPE HTML>
<html>
   <head>
      <script>
         document.getElementsByTagName("eventsource")[0].addEventListener("server-time", eventHandler, false);
         function eventHandler(event)
         {
            // Alert time sent by the server
            document.querySelector('#ticker').innerHTML = event.data;
         }
      </script>
   </head>
   <body>
      <div id = "sse">
         <eventsource src = "/cgi-bin/ticker.cgi" />
      </div>
      <div id = "ticker" name = "ticker">
         [TIME]
      </div>
   </body>
</html>
raja
Published on 10-Apr-2018 13:10:25
Advertisements