Created
December 5, 2014 13:42
-
-
Save Manuel-S/2011ae7e8ce61e986482 to your computer and use it in GitHub Desktop.
Syncs a textbox over different windows without a server using localStorage
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
<html lang="en"> | |
<head> | |
<title>Syncbox</title> | |
</head> | |
<body> | |
<textarea id="sync" style="width:100%; height:100%; overflow:auto; border:0;"></textarea> | |
<script> | |
if(window.localStorage){ | |
var sync = document.getElementById("sync"); | |
var key = 'sync'; | |
addEventListener('storage', handleLocalStorageEvent, false); | |
function handleLocalStorageEvent(e){ | |
if(e.key == key && !document.hasFocus()){ | |
sync.innerText = e.newValue; | |
} | |
} | |
sync.addEventListener("keyup", function() { | |
localStorage.setItem(key, sync.innerText); | |
}); | |
// Initial value from storage | |
sync.innerText = localStorage.getItem(key); | |
sync.focus(); | |
sync.setSelectionRange(sync.value.length,sync.value.length); | |
} | |
else{ | |
alert("localStorage is deactivated!"); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment