如何在 HTML5 中处理服务器发送的事件?
要处理 HTML5 中的服务器发送事件,可以尝试运行以下代码
<!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>
广告