Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chr15m/37e48a4da1dd2f2882bb4ca7ae477afe to your computer and use it in GitHub Desktop.
Save chr15m/37e48a4da1dd2f2882bb4ca7ae477afe to your computer and use it in GitHub Desktop.
Demo of communicating between tabs via the localStorage storage event
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1" name="viewport">
<style>
body {
font-family: Arial, Sans-serif;
max-width: 600px;
margin: 1em auto;
}
</style>
</head>
<body>
<a href="">open another tab (right click)</a>
<button onclick="send();">start</button>
<script>
window.addEventListener('storage', console.log, false);
function send() {
localStorage.setItem("ping", Math.random());
localStorage.setItem("ping", "null");
setTimeout(send, Math.random() * 3000);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment