--- id: html-web-storage title: "HTML Web Storage" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["Web Storage API", "localStorage", "sessionStorage", "HTML5 storage", "client-side storage"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.90 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["html", "web", "frontend", "w3schools", "web-storage", "localstorage", "sessionstorage", "html5"] raw_sources: ["https://www.w3schools.com/html/html5_webstorage.asp"] applied_in: [] github_commit: "" --- # [[HTML Web Storage]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) HTML Web Storage lets applications store key/value data locally in the browser β€” `localStorage` with no expiration and `sessionStorage` for a single tab session β€” with a far larger limit than cookies (at least 5MB) and without ever sending the data to the server. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Web storage** β€” stores data locally within the user's browser; more secure than cookies and capable of holding large amounts of data without affecting website performance. [S1] - **`window.localStorage`** β€” stores data with no expiration date; data is not lost when the browser tab is closed. [S1] - **`window.sessionStorage`** β€” stores data for one session; data is lost when the browser tab is closed. [S1] - **Per origin** β€” web storage is per origin (per domain and protocol); all pages from one origin can store and access the same data. [S1] - **Never sent to server** β€” unlike cookies, the information is never transferred to the server, and the storage limit is far larger (at least 5MB). [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Feature-detect with `typeof(Storage)`** β€” `if (typeof(Storage) !== "undefined") { ... }`. [S1] - **`setItem` / `getItem` / `removeItem`** β€” create, read, and delete name/value pairs. [S1] - **Values are strings** β€” name/value pairs are always stored as strings; convert to other formats as needed. [S1] - **Counter idiom** β€” read a stored count, increment with `Number(...)+1`, and write it back. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) **What is HTML Web Storage?** With web storage, applications can store data locally within the user's browser. Before HTML5, application data had to be stored in cookies, included in every server request. Web storage is more secure, and large amounts of data can be stored locally, without affecting website performance. Unlike cookies, the storage limit is far larger (at least 5MB) and information is never transferred to the server. Web storage is per origin (per domain and protocol); all pages from one origin can store and access the same data. [S1] **The two storage objects** [S1] - `window.localStorage` β€” stores data with no expiration date (data is not lost when the browser tab is closed). - `window.sessionStorage` β€” stores data for one session (data is lost when the browser tab is closed). **Browser support** | API | Chrome | IE | Firefox | Safari | Opera | |---|---|---|---|---|---| | localStorage | 4.0 | 8.0 | 3.5 | 4.0 | 11.5 | | sessionStorage | 4.0 | 8.0 | 3.5 | 4.0 | 11.5 | **Feature detection** ```javascript const x = document.getElementById("result"); if (typeof(Storage) !== "undefined") { x.innerHTML = "Your browser supports Web storage!"; } else { x.innerHTML = "Sorry, no Web storage support!"; } ``` **The localStorage object** ```javascript const x = document.getElementById("result"); if (typeof(Storage) !== "undefined") { // Store localStorage.setItem("lastname", "Smith"); localStorage.setItem("bgcolor", "yellow"); // Retrieve x.innerHTML = localStorage.getItem("lastname"); x.style.backgroundColor = localStorage.getItem("bgcolor"); } else { x.innerHTML = "Sorry, no Web storage support!"; } ``` Example explained: [S1] - Use the `localStorage.setItem()` method to create name/value pairs. - Use the `localStorage.getItem()` method to retrieve the values set. - Retrieve the value of "lastname" and insert it into an element with `id="result"`. - Retrieve the value of "bgcolor" and insert it into the style `backgroundColor` of the element with `id="result"`. To remove an item: ```javascript localStorage.removeItem("lastname"); ``` **Note:** Name/value pairs are always stored as strings. Remember to convert them to another format when needed! [S1] **Counting clicks with localStorage** ```javascript function clickCounter() { const x = document.getElementById("result"); if (typeof(Storage) !== "undefined") { if (localStorage.clickcount) { localStorage.clickcount = Number(localStorage.clickcount)+1; } else { localStorage.clickcount = 1; } x.innerHTML = "You have clicked the button " + localStorage.clickcount + " time(s)!"; } else { x.innerHTML = "Sorry, no Web storage support!"; } } ``` Because this uses `localStorage`, the count persists even after the browser is closed and reopened. [S1] **The sessionStorage object** The `sessionStorage` object is equal to the `localStorage` object, **except** that it stores the data for only one session β€” the data is deleted when the user closes the specific browser tab. [S1] ```javascript function clickCounter() { const x = document.getElementById("result"); if (typeof(Storage) !== "undefined") { if (sessionStorage.clickcount) { sessionStorage.clickcount = Number(sessionStorage.clickcount)+1; } else { sessionStorage.clickcount = 1; } x.innerHTML = "You have clicked the button " + sessionStorage.clickcount + " time(s) in this session!"; } else { x.innerHTML = "Sorry, no Web storage support!"; } } ``` ## πŸ› οΈ 적용 사둀 (Applied in summary) The applied cases are the persistent name/value store (lastname + bgcolor), the persistent click counter (`localStorage`), and the per-session click counter (`sessionStorage`). No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Store and retrieve (JavaScript): ```javascript localStorage.setItem("lastname", "Smith"); const name = localStorage.getItem("lastname"); ``` Remove an item (JavaScript): ```javascript localStorage.removeItem("lastname"); ``` Per-session counter increment (JavaScript): ```javascript if (sessionStorage.clickcount) { sessionStorage.clickcount = Number(sessionStorage.clickcount)+1; } else { sessionStorage.clickcount = 1; } ``` ## βš–οΈ 비ꡐ 및 선택 κΈ°μ€€ (Comparison & decision criteria) The page presents two storage objects with the same API but different lifetimes: [S1] | Object | Expiration / lifetime | Use when | |---|---|---| | `localStorage` | No expiration; survives tab close | Data should persist across sessions | | `sessionStorage` | One session; deleted when the tab is closed | Data should only live for the current tab session | Both are per origin, store strings, and offer at least 5MB versus the much smaller, server-transmitted cookies. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. Practical caveat: values are always strings, so numeric counters use `Number(...)` conversion. [S1] ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[HTML Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[HTML Web APIs]], [[HTML Geolocation]], [[HTML Web Workers]], [[HTML SSE]] - **μ°Έμ‘° λ§₯락:** Referenced whenever an app needs to persist user data client-side without cookies or a server round-trip. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” HTML Web Storage β€” https://www.w3schools.com/html/html5_webstorage.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "HTML Web Storage" page (Astra wiki-curation, P-Reinforce v3.1 format).