Advertisement
STANAANDREY

broadcastch

Jan 2nd, 2022
1,356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8.     <title>Document</title>
  9. </head>
  10.  
  11. <body>
  12.  
  13. </body>
  14. <script>
  15.     const bc = new BroadcastChannel("bc");
  16.  
  17.     bc.onmessage = (event) => {
  18.         if (event.data === "Am I the first?") {
  19.             bc.postMessage(`No you're not.`);
  20.            alert("Another tab of this site just got opened");
  21.        }
  22.        if (event.data === `No you're not.`) {
  23.             alert("An instance of this site is already running");
  24.         }
  25.     };
  26.  
  27.     bc.postMessage('Am I the first?');
  28. </script>
  29.  
  30. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement