250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- 비전공 개발자
- iPhone
- keyframes
- 자바스크립트
- button
- xcode
- effect
- front-end
- IOS
- react
- 개발자
- hover
- javascript
- Animation
- ipad
- 백엔드
- 풀스택
- jQuery
- HTML
- SWIFT
- css3
- 비전공자
- image
- CSS
- html5
- 프론트엔드
- 애니메이션
- iOS 개발자
- MAC
- php
Archives
- Today
- Total
비전공자 개발일기
Javascript Login with LocalStorage 본문
728x90
SMALL
const loginForm = document.querySelector("#login-form");
const loginInput = document.querySelector("#login-form input");
const greeting = document.querySelector("#greeting");
const HIDDEN_CLASSNAME = "hidden"; // 문자열 타입을 변수로 만든 이유 -> 오타로 인한 오류 방지
const USERNAME_KEY = "username";
function onLoginSubmit(event) {
event.preventDefault(); // 이벤트의 기본 진행 차단
loginForm.classList.add(HIDDEN_CLASSNAME);
const username = loginInput.value;
localStorage.setItem(USERNAME_KEY, username);
paintGreetings(username);
}
function paintGreetings(username) {
greeting.innerText = `Hello ${username}`;
greeting.classList.remove(HIDDEN_CLASSNAME);
}
const savedUsername = localStorage.getItem(USERNAME_KEY); //localStorage.setItem("key","value"), getItem("key"),removeItem("key")
if (savedUsername === null) {
loginForm.classList.remove(HIDDEN_CLASSNAME);
loginForm.addEventListener("submit", onLoginSubmit);
} else {
paintGreetings(savedUsername);
}
728x90
LIST
'Javascript' 카테고리의 다른 글
HTML JAVASCRIPT Select option Multiple (0) | 2021.08.27 |
---|---|
Javascript Clock (0) | 2021.08.26 |
Javascript (0) | 2021.08.16 |
Javascript (0) | 2021.08.14 |
Javascript (0) | 2021.08.13 |