Source Code:
(back to article)
Submit
Result:
Report an issue
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Broadcast Channel Example</title> </head> <body> <h1>Broadcast Channel Example</h1> <button id="sendMessage">Send Message</button> <div id="broadcastDisplay"></div> <script> const channel = new BroadcastChannel('example_channel'); channel.onmessage = (event) => { document.getElementById('broadcastDisplay').innerText = 'Broadcast message received: ' + event.data; }; document.getElementById('sendMessage').addEventListener('click', () => { channel.postMessage('Hello from another context!'); }); </script> </body> </html>