์ด์ ์์ฑํ๋ clock.js๋ฅผ ๋ซ๊ณ , greeting.js๋ฅผ ์๋ก ์์ฑํ์ฌ index.html์ ์ถ๊ฐํด ์ฃผ์๋ค. ๋ ๊ทธ๋ ๋ฏ ์์์ divide๋ถํฐ
<script src="clock.js"></script>
<script src="greeting.js"></script>
ํฌ๋กฌ์์ ๊ฐ๋ฐ์ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ ๋, Elements, Console๊ณผ ๊ฐ์ ๋ฉ๋ด์์ Application์ ํญํด๋ณด๋ฉด, Storage ํญ๋ชฉ ์ค Local Storage๊ฐ ์๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
Local Storage๋ ์์ Javascript ์ ๋ณด๋ค์ ๊ฐ URLs ๋ณ๋ก ์ฌ์ฉ์์๊ฒ ์ ์ฅํด ๋๋ ๊ฒ์ ๋งํ๊ณ (์ฆ, velog์ local storage์ ์ ์ฅ๋ ๊ฐ์ naver์์ ๋ถ๋ฌ์ค๋ ๊ฒ์ ๋ถ๊ฐ๋ฅ ํ๋ค๋ ์๋ฏธ์ด๋ค), ์๋ก๊ณ ์นจ์ ํด๋ ๋ณํ์ง ์๋๋ค.
momentum์์ ์ฌ์ฉ์์ ์ด๋ฆ์ ์
๋ ฅ๋ฐ์, ์๋ก๊ณ ์นจ ํ ๋๋ง๋ค ๋ฌผ์ด๋ณด๋ ์ผ์ด ๋ฐ์ํ์ง ์๋๋ก, form๊ณผ input์ ํตํด ์
๋ ฅ๋ฐ์ local storage์ ์ ์ฅํด ๋ณผ ํ
๋ฐ, ๊ทธ ์ ์ Local Storage๊ฐ ์๋ํ๋๋ก ํ๋ ๋งค์ปค๋์ฆ์ ์ฌ์ฉํด๋ณด์.
<form class="js-form form">
<input type="text" placeholder="What is your name?"/>
</form>
<h4 class="js-greetings greetings"></h4>
์ด๋ฌํ ํํ๋ก ์ฌ์ฉ์์ ์ ๋ ฅ์ ๋ฐ๊ณ ์ ํ๋ค. class์ค js-๊ฐ ๋ถ์ class๋ .js์ ๋ด๋ถ์์ ํด๋น ํด๋์ค๋ฅผ ์ง์ ํ๊ธฐ ์ํด์, ๋ถ์ง ์์ class๋ css๋ฅผ ํตํด์ ๋ณ๊ฒฝ์ํค๊ธฐ ์ํด ๋ class๋ฅผ ์ง์ ํด์ฃผ์๋ค.
.greetings {
display: none;
}
.showing {
display: block;
}
css์์๋ ์ฌ์ฉ์์ ์ด๋ฆ์ ๋ณด์ด๊ฒ ํ๋ showing์ด๋ผ๋ class๋ฅผ ๋ฏธ๋ฆฌ ์์ฑํด์ฃผ๊ณ , ๊ธฐ์กด์ greetings๋ ๋ณด์ด์ง ์๋๋ก ํด์ฃผ์๋ค.
const USER_LS = "currentUser",
SHOWING_CN = "showing";
function paintGreeting(text) {
greeting.classList.add(SHOWING_CN);
greeting.innerText = `Hello ${text}!`;
}
function loadName() {
const currentUser = localStorage.getItem(USER_LS)
if(currentUser === null){
} else {
paintGreeting(currentUser);
}
}
ํ์ greetings.js์์ loadName()์ ํตํด localStorage์ currentUser๋ผ๋ key์ ๋ํ value๋ฅผ ์ ์ฅํ๊ณ , ๋ง์ฝ ๊ฐ์ด ์กด์ฌํ๋ค๋ฉด, paintGreeting์ currentUser๊ฐ ์ธ์๋ก ๊ฐ๋๋ก ํ์ฌ ์คํ, greeting์ classList์ showing์ ์ถ๊ฐํ์ฌ display: none;
์ display: block;
์ด ๋๋๋ก ํ์๋ค.
๊ฐ๋ฐ์ ๋๊ตฌ๋ฅผ ์ด์ฉํ์ฌ currentUser์ ๊ฐ์ ์ค์ ํด์ค ํ
์๋ก๊ณ ์นจ์ ํ๋ฉด,,
์ ์์ ์ผ๋ก ํจ์๊ฐ ์๋ํจ์ ํ์ธํ ์ ์๋ค!