Skip to content

Instantly share code, notes, and snippets.

@Kcko
Created January 8, 2024 07:49
Show Gist options
  • Save Kcko/63cf68978e6720a85866b99471200887 to your computer and use it in GitHub Desktop.
Save Kcko/63cf68978e6720a85866b99471200887 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button>CLICK ME</button>
<script>
const channel = new BroadcastChannel('myChannel');
document.getElementsByTagName('button')[0].addEventListener('click', e => {
channel.postMessage('Hello from Tab 1' + new Date().getSeconds);
console.log('CHANNEL 1');
});
channel.onmessage = event => {
console.log('Message received in Tab 2:', event.data);
console.log('CHANNEL 2');
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment