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
- flex-basis
- css variables
- Sass
- Object.create
- 무료 백엔드 배포
- css
- Node.js
- shit-christmas
- express-handlebars
- es6
- just-one-small-sip
- css grid
- 정규표현식
- select by attribute
- node
- ajax
- flex-shrink
- img 확대
- module wrapper function
- close together
- css 오버레이
- Prototype
- regExp
- Express
- flexbox
- Engoo
- JS
- flex-grow
- improve-iq-by-up-to-10%!
- 디자인패턴
Archives
- Today
- Total
Local Storage & Session 본문
Local Storage
key value pair 이며 String으로만 저장 가능하다. 따라서 배열이나 객체 등은 JSON.stringify 메서드를 이용해 문자열로 변경후 저장한다. 데이터를 삭제하려면 수동으로 지워야 한다. (세션이 종료되어도 데이터가 남아있음)
Session Storage
Local Storage와 비슷하나 다른점은 세션이 종료될 때 데이터가 없어진다는 점이다.
// set local storage item
// localStorage.setItem("name", "John");
// localStorage.setItem("age", "30");
// set session storage item
// sessionStorage.setItem("name", "Beth");
// remove from storage
// localStorage.removeItem("name");
// get from storage
// const name = localStorage.getItem("name");
// const age = localStorage.getItem("age");
// // clear local storage
// localStorage.clear();
// console.log(name, age);
document.querySelector("form").addEventListener("submit", function(e) {
const task = document.getElementById("task").value;
let tasks;
if(localStorage.getItem("tasks") === null) {
tasks = [];
} else {
tasks = JSON.parse(localStorage.getItem("tasks"));
}
tasks.push(task);
localStorage.setItem("tasks", JSON.stringify(tasks));
alert("Task saved");
e.preventDefault();
});
const tasks = JSON.parse(localStorage.getItem("tasks"));
tasks.forEach(function(task) {
console.log(task);
});
'2021 프론트 엔드 로드맵 따라가기 > JS' 카테고리의 다른 글
특정 범위의 랜덤 정수 값 생성 코드 (0) | 2021.06.04 |
---|---|
Number()와 parseInt() 차이 (0) | 2021.06.04 |
Event Bubbling & Delegation (0) | 2021.05.31 |
Key & Input events (0) | 2021.05.31 |
Mouse events (0) | 2021.05.31 |
Comments