Created
February 23, 2011 09:26
-
-
Save azproduction/840221 to your computer and use it in GitHub Desktop.
LocalStorage broadcast
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
<body> | |
<h1>localStorage window broadcast example</h1> | |
<script> | |
function openwindows(n){ | |
var h = window.screen.height / n, | |
w = window.screen.width / n, i, j; | |
for (i = 0; i < n; i += 1) { | |
for (j = 0; j < n; j += 1) { | |
window.open('window.html', '_blank', "width=" + (w - 10) + ",height=" + (h - 60) + ",resizable=no,scrollbars=no,status=no,left=" + (i * w) + ",top=" + (j * h)); | |
} | |
} | |
} | |
function openwindow(){ | |
var h = window.screen.height, | |
w = window.screen.width; | |
window.open('window.html', '_blank', "width=200,height=200,resizable=no,scrollbars=no,status=no,left=" + (~~(Math.random()*w)) + ",top=" + (~~(Math.random()*h))); | |
} | |
</script> | |
<button onclick="openwindow();">Open 1 window</button> | |
<button onclick="openwindows(4);">Open 16 windows</button> | |
<button onclick="openwindows(5);">Open 25 windows</button> | |
</body> |
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
<title>Broadcast target</title> | |
<body> | |
<script> | |
function onStorage() { // Note: IE and FF lags on large numbers of windows | |
// Note: IE limits/delays broadcast range (number of windows) | |
var cmd = localStorage.getItem('command'); | |
document.getElementById("result").innerHTML = cmd; | |
if (cmd.match(/close/)) { | |
window.close(); | |
} else { | |
document.body.style.backgroundColor = 'rgb(' + ~~(cmd * 255) + ',' + ~~(cmd * 255) + ',' + ~~(cmd * 255) + ')'; | |
document.body.style.color = 'rgb(' + ~~(Math.random() * 255) + ',' + ~~(Math.random() * 255) + ',' + ~~(Math.random() * 255) + ')'; | |
} | |
} | |
var webkit = !!navigator.userAgent.match(/AppleWebKit\/(\d+\.\d+)/); | |
if ('v'=='\v') { // Note: IE listens on document | |
document.attachEvent('onstorage', onStorage, false); | |
} else if (window.opera || webkit){ // Note: Opera and WebKits listens on window | |
window.addEventListener('storage', onStorage, false); | |
} else { // Note: FF listens on document.body or document | |
document.body.addEventListener('storage', onStorage, false); | |
} | |
function broadcast(cmd){ | |
localStorage.setItem('command', cmd); | |
if (window.opera || webkit) { | |
// Note: Opera and WebKits don't fire storage event on event source window | |
// Do it manually | |
onStorage(); | |
} | |
} | |
</script> | |
<button onclick="broadcast(Math.random());">Broadcast</button> | |
<button onclick="broadcast('close-'+Math.random());">Close all</button> | |
<pre id="result"></pre> | |
</body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment