Created
June 8, 2018 12:43
-
-
Save chr15m/37e48a4da1dd2f2882bb4ca7ae477afe to your computer and use it in GitHub Desktop.
Demo of communicating between tabs via the localStorage storage event
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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