Web Storage APIλ λΈλΌμ°μ κ° μΏ ν€λ₯Ό μ¬μ©νλ κ²λ³΄λ€ ν¨μ¬ λ μ§κ΄μ μΈ λ°©λ²μΌλ‘ ν€-κ° μμ μμ νκ² μ μ₯ν μ μλ λ©μ»€λμ¦μ μ 곡νλ ν©λλ€. mdn-using-the-web-storage-api
λ‘컬 μ μ₯μλ μΏ ν€λ³΄λ€ μ μ₯ μ©λμ΄ ν¬λ©°, μ μ₯λ λ°μ΄ν°λ₯Ό μλ²λ‘ μ μ‘νμ§ μμ΅λλ€.
Web Storage APIμ Storage μΈν°νμ΄μ€mdn-storage-interfaceλ νΉμ λλ©μΈμ λν μΈμ μ μ₯μ λλ λ‘컬 μ μ₯μμ λν μ‘μΈμ€λ₯Ό μ 곡νλ©° μ μ₯λ λ°μ΄ν° νλͺ©μ μΆκ°, μμ λλ μμ ν μ μμ΅λλ€.
λμΌ μλ³Έ Origin (λλ©μΈ λ° νλ‘ν μ½)μμλ λ°μ΄ν°λ₯Ό μ μ₯ν νμ΄μ§κ° λ¬λΌλ λμΌν λ‘컬 μ μ₯μμ λν΄ I/Oκ° κ°λ₯ν©λλ€.
Web Storage μΈν°νμ΄μ€λ window κ°μ²΄μ localStorage μ SessionStorage μμ±μΌλ‘ ꡬνλμ΄ μμ΅λλ€.
λ μμ±μ μ μΌν μ°¨μ΄μ μ localStorageμ μ μ₯λ λ°μ΄ν°μλ λ§λ£ μκ°μ΄ μμ§λ§, sessionStorageμ μ μ₯λ λ°μ΄ν°λ λΈλΌμ°μ§ μΈμ μ΄ λλλ©΄ (μ¦, λΈλΌμ°μ κ° λ«ν λ) μ§μμ§λλ€.
Storage Interfacemdn-storage-interface
λ‘컬 μ μ₯μμ λ°μ΄ν°λ₯Ό ν€-κ° ννλ‘ μ μ₯ν©λλ€.
κ°μ λ¬Έμμ΄ νμλ§μ μ§μν©λλ€.
λͺ μΈ Specifications
Storage Interface μ λͺ μΈstorage-interface-sepcificationλ μλμ κ°μ΅λλ€.
interface Storage {
readonly attribute unsigned long length;
DOMString? key(unsigned long index);
getter DOMString? getItem(DOMString key);
setter void setItem(DOMString key, DOMString value);
deleter void removeItem(DOMString key);
void clear();
};
λΈλΌμ°μ μ§μ νμΈ Check Browser support
if (typeof Storage !== "undefined") {
// νμ¬ λΈλΌμ°μ λ Web Storage APIλ₯Ό μ§μν©λλ€.
} else {
// νμ¬ λΈλΌμ°μ λ Web Storage APIλ₯Ό μ§μνμ§ μμ΅λλ€.
}
μμ± Properties
Storage.length
λ‘컬 μ μ₯μμ μ μ₯λ λ°μ΄ν°μ μλ₯Ό κ°μ Έμ΅λλ€.
var storage = window.localStorage;
storage.setItem("key1", "value1");
storage.setItem("key2", "value2");
storage.setItem("key3", "value3");
var countOfKeyValuePairs = storage.length; // 3
λ©μλ Methods
Storage.key(n)
n λ²μ§Έ ν€λ₯Ό λ°νν©λλ€.
var storage = window.localStorage;
storage.setItem("key1", "value1");
storage.setItem("key2", "value2");
storage.setItem("key3", "value3");
storage.key(0); // ==> key1
storage.key(1); // ==> key2
storage.key(2); // ==> key3
Storage.setItem(k, v)
λ‘컬 μ μ₯μμ λ°μ΄ν°λ₯Ό ν€ k
, κ° v
λ‘ μ μ₯ν©λλ€.
μλ‘μ΄ ν€ k
λ‘ λ°μ΄ν°λ₯Ό μ
λ ₯νλ©΄ λ‘컬 μ μ₯μμ μλ‘μ΄ νλͺ©μ΄ μ μ₯λκ³ , μ‘΄μ¬νλ ν€ k
λ₯Ό μ
λ ₯νλ©΄ κΈ°μ‘΄ νλͺ©μ κ°μ΄ κ°±μ λ©λλ€.
var storage = window.localStorage;
storage.setItem("key1", "value1"); // μλ‘μ΄ νλͺ©μ΄ μΆκ°λ¨
storage.setItem("key2", "value2"); // μλ‘μ΄ νλͺ©μ΄ μΆκ°λ¨
storage.setItem("key1", "value3"); // κΈ°μ‘΄ ν€ key1 νλͺ©μ κ°μ΄ 'value3' μΌλ‘ κ°±μ λ¨
Storage.getItem(k)
λ‘컬 μ μ₯μμμ ν€ k
μ ν΄λΉνλ νλͺ©μ κ°μ λ°νν©λλ€.
var storage = window.localStorage;
storage.setItem("key1", "value1");
storage.setItem("key2", "value2");
var val1 = storage.getItem("key1"); // ==> 'value1'
var var2 = storage.getItem("key2"); // ==> 'value2'
var var3 = storage.getItem("key3"); // ==> undefined
Storage.removeItem(k)
λ‘컬 μ μ₯μμμ ν€ k
μ ν΄λΉνλ νλͺ©μ μ κ±°ν©λλ€.
var storage = window.localStorage;
var countOfKeyValuePairs = 0;
storage.setItem("key1", "value1");
storage.setItem("key2", "value2");
countOfKeyValuePairs = storage.length; // 2
storage.removeItem("key1");
countOfKeyValuePairs = storage.length; // 1
Storage.clear()
λ‘컬 μ μ₯μλ₯Ό μ΄κΈ°νν©λλ€.
var storage = window.localStorage;
var countOfKeyValuePairs = 0;
storage.setItem("key1", "value1");
storage.setItem("key2", "value2");
countOfKeyValuePairs = storage.length; // 2
storage.clear();
countOfKeyValuePairs = storage.length; // 0
λΈλΌμ°μ νΈνμ± Browser compatibility
μΉ λΈλΌμ°μ λ§λ€ λ‘컬 μ μ₯μ localStorage, sessionStorage μ μ₯ μμ€μ΄ λ€λ¦ λλ€. Web Storage Support Testweb-storage-support-test μ¬μ΄νΈμμ νμ¬ μΉ λΈλΌμ°μ μ μ μ₯ μμ€μ λλ΅μ μΌλ‘ νμΈν μ μμ΅λλ€.
λ°μ€ν¬ν± Desktop
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|
localStorage | 4 | 3.5 | 8 | 10.50 | 4 |
sessionStorage | 5 | 2 | 8 | 10.50 | 4 |
λͺ¨λ°μΌ Mobile
Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support | 2.1 | ? | 8 | 11 | 3.2ios-5-1-safarimobile |
window.localStorage mdn-window-localstorage
λ§λ£μκ°μ΄ μλ λ°μ΄ν°λ₯Ό ν€-κ° ννλ‘ μ μ₯ν©λλ€.
var storage = window.localStorage;
storage.setItem("key1", "value1");
var val1 = storage.getItem("key1");
window.sessionStorage mdn-window-sessionstorage
λΈλΌμ°μ§ μΈμ μ΄ λλλ©΄ μ΄κΈ°νλλ λ°μ΄ν°λ₯Ό ν€-κ° ννλ‘ μ μ₯ν©λλ€.
var storage = window.sessionStorage;
storage.setItem("key1", "value1");
var val1 = storage.getItem("key1");
- MDN: Using the Web Storage APIβ©
- MDN: storageβ©
- The Storage Interfaceβ©
- Web Storage Support Testβ©
- iOS 5.1λΆν° Safari Mobile μ localStorage λ°μ΄ν°λ₯Ό μΊμ λλ ν°λ¦¬μ μ μ₯ν©λλ€. μΊμ λλ ν°λ¦¬λ μΌλ°μ μΌλ‘ 곡κ°μ΄ λΆμ‘±ν κ²½μ° μμ€ν μ μν΄ μ 리λ μλ μμ΅λλ€.β©
- MDN: window.localStorageβ©
- MDN: window.sessionStorageβ©