localStorage

zooyeongยท2023๋…„ 3์›” 22์ผ
0

8์ฃผ์ฐจ

๋ชฉ๋ก ๋ณด๊ธฐ
4/6
post-thumbnail

๐Ÿ“Œ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€(localStorage)

๋ธŒ๋ผ์šฐ์ € ์ƒ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ธฐ์ˆ ์ธ ์›น ์Šคํ† ๋ฆฌ์ง€์—๋Š” ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€(localStorage)์™€ ์„ธ์…˜ ์Šคํ† ๋ฆฌ์ง€(sessionStorage)๊ฐ€ ์žˆ๋‹ค. ์„ธ์…˜ ์Šคํ† ๋ฆฌ์ง€๋Š” ์›น ํŽ˜์ด์ง€์˜ ์„ธ์…˜์ด ๋๋‚  ๋•Œ ์ €์žฅ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ์ง€์›Œ์ง€๋Š” ๋ฐ˜๋ฉด, ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€๋Š” ์›นํŽ˜์ด์ง€์˜ ์„ธ์…˜์ด ๋๋‚˜๋„ ๋ฐ์ดํ„ฐ๊ฐ€ ์ง€์›Œ์ง€์ง€ ์•Š๋Š”๋‹ค.
์›น ์Šคํ† ๋ฆฌ์ง€๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ‚ค(key)์™€ ๊ฐ’(value)์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

localStorage.setItem("key", "value"); : ๋ฐ์ดํ„ฐ ์ €์žฅ
localStorage.getItem("key", "value"); : ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ(๋ถˆ๋Ÿฌ์˜ค๊ธฐ)
localStorage.removeItem("key"); : ํ•ด๋‹น ๋ฐ์ดํ„ฐ ์‚ญ์ œ
localStorage.clear(); : ๋ชจ๋“  ๋ฐ์ดํ„ฐ ์‚ญ์ œ

์ €์žฅ๋œ ๋กœ์ปฌ ์Šคํ† ๋ฆฌ์ง€์˜ ๋ฐ์ดํ„ฐ๋Š” ๋ธŒ๋ผ์šฐ์ €์˜ ์ฝ˜์†” ์ฐฝ์„ ์—ด์–ด ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

์ฝ˜์†”์ฐฝ

๐Ÿ”ฅ์—ฐ์Šต

/*
  1. ๋กœ๊ทธ์ธ์„ ๋ˆ„๋ฅด๋ฉด ์•„์ด๋””์™€ ๋น„๋ฒˆ์ด ๊นจ๋—ํ•ด์ง„๋‹ค.
  2. ๋กœ๊ทธ์ธ ์‹œ, ์•„์ด๋””์ €์žฅ์„ ์ฒดํฌํ•œ ์ƒํƒœ๋ฉด ๋‹ค์Œ์— ์™”์„ ๋•Œ ์•„์ด๋””๊ฐ€ ๊ธฐ์ž…๋˜์–ด์žˆ๋‹ค.
    -> localStorage ํ™œ์šฉ!
    1) ๋กœ๊ทธ์ธ๋ฒ„ํŠผ ๋ˆŒ๋ €์„ ๋•Œ -> ์ฒดํฌ๋ฐ•์Šค ์ฒดํฌ์—ฌ๋ถ€
    2) ์ฒดํฌ ์ƒํƒœ๋ฉด localStorage์— ์ €์žฅ
    3) ์ด์ „์— ์•„์ด๋””์ €์žฅ์„ ํ–ˆ์œผ๋ฉด, lacalStorage์—์„œ ๋ถˆ๋Ÿฌ์™€์„œ ์•„์ด๋”” ์นธ์— ๊ธฐ์ž….
  3. ์•„์ด๋””๋Š” ํ•œ๊ฐœ๋งŒ ์ €์žฅ๋œ๋‹ค. (์ œ์ผ ๋งˆ์ง€๋ง‰์— ์ €์žฅ๋˜์—ˆ๋˜ ์•„์ด๋””๋กœ ํ™œ์šฉ)
*/

๐Ÿ”ฝHTML ์ฝ”๋“œ

<div class="container">
  <h1>๋กœ๊ทธ์ธ</h1>
  <h3>์•„์ด๋””</h3>
  <input type="text" id="inputId" placeholder="์•„์ด๋””">
  <h3>๋น„๋ฐ€๋ฒˆํ˜ธ</h3>
  <input type="password" id="inputPw"placeholder="๋น„๋ฐ€๋ฒˆํ˜ธ">

  <label for="">์•„์ด๋”” ์ €์žฅ<input type="checkbox" id="checkIdSave"></label>

  <button id="btnLogIn">๋กœ๊ทธ์ธ</button>
</div>

๐Ÿ”ฝCSS ์ฝ”๋“œ

.container{
  width: 400px;
  border: 1px solid black;
  margin: 0 auto;
  padding: 50px;
  display: flex;
  flex-direction: column;
}

.container label{
  margin-top: 20px;
}

.container button{
  margin-top: 20px;
}

.container input{
  font-size: 1.25rem;
  padding-left: 10px;
}

.container input:focus{
  border: 1px solid red;
}

h1{
  text-align: center;
}

๐Ÿ”ฝJS ์ฝ”๋“œ

window.onload = ()=>{

  const btn = document.getElementById('btnLogIn');
  const inputId = document.getElementById('inputId');
  const inputPw = document.getElementById('inputPw');
  const inputChk = document.getElementById('checkIdSave');
  
  inputId.value = localStorage.getItem("id");

  btn.addEventListener('click', function(){
  	if(inputChk.checked == true){
      localStorage.setItem("id", inputId.value);
  	}
  	inputId.value = '';
  	inputPw.value = '';
  });
  
}

๐Ÿšฉ์•„์ด๋”” ์ €์žฅ ์ฒดํฌ ํ›„ ์‹คํ–‰๊ฒฐ๊ณผ

์ฝ˜์†”์ฐฝ๊ฒฐ๊ณผ
/*
  *์ถ”๊ฐ€์š”๊ตฌ์‚ฌํ•ญ
  4. ์•„์ด๋”” ์ €์žฅ์„ ํ’€๊ณ  ๋กœ๊ทธ์ธํ•˜๋ฉด, ์•„์ด๋””๊ฐ€ ์ €์žฅ๋˜์ง€ ์•Š๋Š”๋‹ค.
  5. ์ €์žฅ๋œ ์•„์ด๋””๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ํ‘œ๊ธฐํ•œ ๊ฒฝ์šฐ์—๋Š” ์•„์ด๋”” ์ €์žฅ ์ฒดํฌ๋ฐ•์Šค๋„ ๊ฐ™์ด ์ฒดํฌ๋œ ์ƒํƒœ๋กœ ์‹œ์ž‘ํ•œ๋‹ค.
*/

๐Ÿ”ฝJS ์ฝ”๋“œ ์ถ”๊ฐ€ ๋ฐ ๋ณ€๊ฒฝ

window.onload = ()=>{

  const btn = document.getElementById('btnLogIn');
  const inputId = document.getElementById('inputId');
  const inputPw = document.getElementById('inputPw');
  const inputChk = document.getElementById('checkIdSave');

  let id_localStorage = localStorage.getItem("id");
  inputId.value = id_localStorage;
  if(id_localStorage != '' && id_localStorage != null){
    inputChk.checked = true;
  }

  btn.addEventListener('click', function(){
    if(inputChk.checked == true){
      localStorage.setItem("id", inputId.value);
    } else{
      localStorage.setItem("id", "");
      // localStorage.removeItem("id"); -> ๊ฐ’์ด null์ด ๋จ
    }
    inputId.value = '';
    inputPw.value = '';
  });
}

๐Ÿšฉ์‹คํ–‰ ๊ฒฐ๊ณผ

์ฝ˜์†”์ฐฝ๊ฒฐ๊ณผ
profile
Have a good day โŒฏโ€™โ–พโ€™โŒฏ

0๊ฐœ์˜ ๋Œ“๊ธ€