Skip to content

Instantly share code, notes, and snippets.

@Manuel-S
Created December 5, 2014 13:42
Show Gist options
  • Save Manuel-S/2011ae7e8ce61e986482 to your computer and use it in GitHub Desktop.
Save Manuel-S/2011ae7e8ce61e986482 to your computer and use it in GitHub Desktop.
Syncs a textbox over different windows without a server using localStorage
<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