ReasonJun

javascript : Web APIs (storage) 본문

Frontend/Javasciprt

javascript : Web APIs (storage)

ReasonJun 2023. 6. 9. 00:18
728x90

In web development, the "Web Storage" API provides a way to store data locally in the user's web browser. It consists of two mechanisms: localStorage and sessionStorage. Both mechanisms offer a simple key-value storage interface and are supported by modern web browsers.

Here are the key points about Web Storage:

  1. localStorage: The localStorage object allows you to store key-value pairs persistently in the user's browser. The data stored in localStorage remains even after the browser is closed and reopened. This makes it suitable for long-term data storage, such as user preferences or cached data.
  2. sessionStorage: The sessionStorage object is similar to localStorage, but it stores data only for the duration of the current browser session. When the session ends (e.g., the browser is closed), the data is cleared. sessionStorage is useful for storing temporary data that is only relevant during a particular browsing session.
  3. Storage Limitations: Each origin (combination of protocol, domain, and port) has its own separate storage for localStorage and sessionStorage. The maximum storage capacity varies across browsers but is typically around 5 MB or more. Keep in mind that exceeding the storage limit may result in errors or data loss.
  4. API Usage: Both localStorage and sessionStorage share a similar API. You can set and retrieve data using the setItem(key, value) and getItem(key) methods, respectively. You can also remove an item with the removeItem(key) method or clear all data with the clear() method.
// Storing data in localStorage
localStorage.setItem('username', 'John');
const username = localStorage.getItem('username');
console.log(username); // Output: "John"

// Storing data in sessionStorage
sessionStorage.setItem('theme', 'dark');
const theme = sessionStorage.getItem('theme');
console.log(theme); // Output: "dark"
  1. Data Format: Both localStorage and sessionStorage store data as strings. If you need to store complex data structures such as objects or arrays, you can use JSON serialization and deserialization.
const user = {
  name: 'John',
  age: 30,
};

// Storing an object in localStorage
localStorage.setItem('user', JSON.stringify(user));

// Retrieving and parsing the object from localStorage
const storedUser = JSON.parse(localStorage.getItem('user'));
console.log(storedUser.name); // Output: "John"
  1. Cross-Origin Restrictions: Web Storage follows the same-origin policy, which means that localStorage and sessionStorage data can only be accessed by web pages from the same origin (protocol, domain, and port combination). This prevents scripts from different origins from accessing each other's stored data.

Web Storage provides a simple and convenient way to store and retrieve data on the client-side. It is widely supported by modern browsers and offers an alternative to cookies for local data storage. However, it's important to note that Web Storage is not suitable for storing sensitive or critical data, as it is accessible to scripts running on the same domain. For sensitive data, consider using server-side storage or encryption techniques.

728x90

'Frontend > Javasciprt' 카테고리의 다른 글

javascript : Web API (location)  (0) 2023.06.09
javascript : Web API (history)  (0) 2023.06.09
javascript : Web APIs (cookie)  (0) 2023.06.09
javascript : Web APIs (console)  (0) 2023.06.09
javascript : symbol  (0) 2023.06.09
Comments