Skip to content

Instantly share code, notes, and snippets.

@esctabcapslock
Created October 19, 2021 01:53
Show Gist options
  • Save esctabcapslock/165698741326effba9776e31f8406956 to your computer and use it in GitHub Desktop.
Save esctabcapslock/165698741326effba9776e31f8406956 to your computer and use it in GitHub Desktop.
브라우저 localStorage에 값을 저장해 자동 저장 기능 구현
<!DOCTYPE HTML>
<html lang="ko">
<head>
<meta charset="UTF-8">
</head>
<body>
<textarea id="txt_area" style="height:100px; width:100%"></textarea>
<h2 id="ttt"></h2>
<script>
const txt = document.getElementById('txt_area');
const h2 = document.getElementById('ttt');
function push_data () {
h2.innerHTML = (txt.value = (localStorage.x || "")).length
}
function pull_data () {
h2.innerHTML = (sessionStorage.x = localStorage.x = txt.value).length;
}
//|| sessionStorage.x는 다른 창에서 적용되지 않음 bbbb
push_data();
txt.addEventListener('keyup',pull_data);
txt.addEventListener('keydown',pull_data);
txt.addEventListener('click',pull_data);
txt.addEventListener('focus',pull_data);
txt.addEventListener('change',pull_data);
setInterval(() => {
if(sessionStorage.x != localStorage.x) push_data();
}, 100);
// window.addEventListener("localDataStorage", function (e) {
// // do your checks to detect
// console.log('@#',e)
// // changes in "e1", "e2" & "e3" here
// }, false);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment