๐ฉ Local Storage
- ์ ์ฅ๋ ๋ฐ์ดํฐ๋ ๋ธ๋ผ์ฐ์ ๋ฅผ ๋ซ์๋ ๊ณ์ ์ ์ง๋ฉ๋๋ค.
- ์ค๋ฆฌ์ง(domainยทportยทprotocol)์ด ๊ฐ์ ํญ, ์ฐฝ ์ ์ฒด์์ ๊ณต์
- ํ ์ฐฝ์ ๋ฐ์ดํฐ๋ฅผ ์ค์ ํ๋ฉด ๋ค๋ฅธ ์ฐฝ์์ ๋ณ๋ ์ฌํญ์ ๋ณผ ์ ์์ต๋๋ค.
๐ฉ Session Storage
- ์ ์ฅ๋ ๋ฐ์ดํฐ๋ ๋ธ๋ผ์ฐ์ ๋ฅผ ๋ซ๊ฑฐ๋ ํญ์ ๋ซ์ผ๋ฉด ์ญ์ ๋ฉ๋๋ค.
- ์ค๋ฆฌ์ง์ด ๊ฐ์ ๋ธ๋ผ์ฐ์ ํญ, iframe์์ ๊ณต์
- ํ์ฌ ๋ ์๋ ํญ ๋ด์์๋ง ๋ฐ์ดํฐ๊ฐ ์ ์ง๋ฉ๋๋ค.
๐ฉ API
๋ ์คํ ๋ฆฌ์ง ๊ฐ์ฒด๋ ๋์ผํ ๋ฉ์๋์ ํ๋กํผํฐ๋ฅผ ์ ๊ณตํฉ๋๋ค.
Storage.setItem()
- setItem(key, value): ํค-๊ฐ ์์ ๋ณด๊ดํฉ๋๋ค.
- getItem(key): ํค์ ํด๋นํ๋ ๊ฐ์ ๋ฐ์์ต๋๋ค.
- removeItem(key): ํค์ ํด๋น ๊ฐ์ ์ญ์ ํฉ๋๋ค.
- clear(): ๋ชจ๋ ๊ฒ์ ์ญ์ ํฉ๋๋ค.
๐ฉ ๊ฐ์ฒด ์ ์ฅ
ํค์ ๊ฐ์ ๋ฐ๋์ ๋ฌธ์์ด์ด์ด์ผ ํฉ๋๋ค. ์ซ์๋ ๊ฐ์ฒด ๋ฑ ๋ค๋ฅธ ์๋ฃํ์ ์ฌ์ฉํ๋ฉด ๋ฌธ์์ด๋ก ์๋ ๋ณํ๋ฉ๋๋ค.
localStorage.setItem('object', { name: 'John' });
localStorage.getItem('object'); // [object Object]
๋ค์ ๋ฐฉ์์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์ ์ฅํ ์ ์์ต๋๋ค.
localStorage.setItem('object', JSON.stringify({ name: 'John' }));
JSON.parse(localStorage.getItem('object')); // { name: 'John' }
๐ฉ storage ์ด๋ฒคํธ
Local Storage๋ Session Storage ๋ฐ์ดํฐ๊ฐ ๊ฐฑ์ ๋ ๋, storage ์ด๋ฒคํธ๊ฐ ์คํ๋ฉ๋๋ค. ๋ค์ ํ๋กํผํฐ๋ฅผ ์ง์ํฉ๋๋ค.
- key: ๋ณ๊ฒฝ๋ ๋ฐ์ดํฐ์ ํค(.clear()๋ฅผ ํธ์ถํ๋ค๋ฉด null)
- oldValue: ์ด์ ๊ฐ(ํค๊ฐ ์๋กญ๊ฒ ์ถ๊ฐ๋์๋ค๋ฉด null)
- newValue: ์๋ก์ด ๊ฐ(ํค๊ฐ ์ญ์ ๋์๋ค๋ฉด null)
- url: ๊ฐฑ์ ์ด ์ผ์ด๋ ๋ฌธ์์ url
- storageArea: ๊ฐฑ์ ์ด ์ผ์ด๋ localStorage๋ sessionStorage ๊ฐ์ฒด
storage ์ด๋ฒคํธ๋ ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํจ ์คํ ๋ฆฌ์ง๋ฅผ ์ ์ธํ๊ณ ์คํ ๋ฆฌ์ง์์ ์ ๊ทผ ๊ฐ๋ฅํ window ๊ฐ์ฒด ์ ๋ถ์์ ์ผ์ด๋ฉ๋๋ค.
- ๋ ๊ฐ์ ์ฐฝ์ ๊ฐ์ ์ฌ์ดํธ๋ฅผ ๋์๋๊ณ Local Storage ๋ฐ์ดํฐ ๋ณ๊ฒฝ
- ๋ ์ฐฝ์์ ๋ชจ๋ storage ์ด๋ฒคํธ๋ฅผ ์์ ํ๊ณ ์๊ธฐ ๋๋ฌธ์ ํ ์ฐฝ์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐฑ์ ํ๋ฉด ๋ค๋ฅธ ์ฐฝ์์ ํด๋น ์ฌํญ์ด ๋ฐ์๋๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
window.addEventListener('storage', function (e) {
if (e.key === 'myData') {
console.log('Key: ' + e.key);
console.log('Old Value: ' + e.oldValue);
console.log('New Value: ' + e.newValue);
console.log('Storage Area: ' + e.storageArea);
console.log('URL: ' + e.url);
}
});
๐ฉ ์ฐธ๊ณ
- https://ko.javascript.info/localstorage#ref-785
- https://www.zerocho.com/category/HTML&DOM/post/5918515b1ed39f00182d3048
- https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage
๐ฉ Local Storage
- ์ ์ฅ๋ ๋ฐ์ดํฐ๋ ๋ธ๋ผ์ฐ์ ๋ฅผ ๋ซ์๋ ๊ณ์ ์ ์ง๋ฉ๋๋ค.
- ์ค๋ฆฌ์ง(domainยทportยทprotocol)์ด ๊ฐ์ ํญ, ์ฐฝ ์ ์ฒด์์ ๊ณต์
- ํ ์ฐฝ์ ๋ฐ์ดํฐ๋ฅผ ์ค์ ํ๋ฉด ๋ค๋ฅธ ์ฐฝ์์ ๋ณ๋ ์ฌํญ์ ๋ณผ ์ ์์ต๋๋ค.
๐ฉ Session Storage
- ์ ์ฅ๋ ๋ฐ์ดํฐ๋ ๋ธ๋ผ์ฐ์ ๋ฅผ ๋ซ๊ฑฐ๋ ํญ์ ๋ซ์ผ๋ฉด ์ญ์ ๋ฉ๋๋ค.
- ์ค๋ฆฌ์ง์ด ๊ฐ์ ๋ธ๋ผ์ฐ์ ํญ, iframe์์ ๊ณต์
- ํ์ฌ ๋ ์๋ ํญ ๋ด์์๋ง ๋ฐ์ดํฐ๊ฐ ์ ์ง๋ฉ๋๋ค.
๐ฉ API
๋ ์คํ ๋ฆฌ์ง ๊ฐ์ฒด๋ ๋์ผํ ๋ฉ์๋์ ํ๋กํผํฐ๋ฅผ ์ ๊ณตํฉ๋๋ค.
Storage.setItem()
- setItem(key, value): ํค-๊ฐ ์์ ๋ณด๊ดํฉ๋๋ค.
- getItem(key): ํค์ ํด๋นํ๋ ๊ฐ์ ๋ฐ์์ต๋๋ค.
- removeItem(key): ํค์ ํด๋น ๊ฐ์ ์ญ์ ํฉ๋๋ค.
- clear(): ๋ชจ๋ ๊ฒ์ ์ญ์ ํฉ๋๋ค.
๐ฉ ๊ฐ์ฒด ์ ์ฅ
ํค์ ๊ฐ์ ๋ฐ๋์ ๋ฌธ์์ด์ด์ด์ผ ํฉ๋๋ค. ์ซ์๋ ๊ฐ์ฒด ๋ฑ ๋ค๋ฅธ ์๋ฃํ์ ์ฌ์ฉํ๋ฉด ๋ฌธ์์ด๋ก ์๋ ๋ณํ๋ฉ๋๋ค.
localStorage.setItem('object', { name: 'John' });
localStorage.getItem('object'); // [object Object]
๋ค์ ๋ฐฉ์์ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด๋ฅผ ์ ์ฅํ ์ ์์ต๋๋ค.
localStorage.setItem('object', JSON.stringify({ name: 'John' }));
JSON.parse(localStorage.getItem('object')); // { name: 'John' }
๐ฉ storage ์ด๋ฒคํธ
Local Storage๋ Session Storage ๋ฐ์ดํฐ๊ฐ ๊ฐฑ์ ๋ ๋, storage ์ด๋ฒคํธ๊ฐ ์คํ๋ฉ๋๋ค. ๋ค์ ํ๋กํผํฐ๋ฅผ ์ง์ํฉ๋๋ค.
- key: ๋ณ๊ฒฝ๋ ๋ฐ์ดํฐ์ ํค(.clear()๋ฅผ ํธ์ถํ๋ค๋ฉด null)
- oldValue: ์ด์ ๊ฐ(ํค๊ฐ ์๋กญ๊ฒ ์ถ๊ฐ๋์๋ค๋ฉด null)
- newValue: ์๋ก์ด ๊ฐ(ํค๊ฐ ์ญ์ ๋์๋ค๋ฉด null)
- url: ๊ฐฑ์ ์ด ์ผ์ด๋ ๋ฌธ์์ url
- storageArea: ๊ฐฑ์ ์ด ์ผ์ด๋ localStorage๋ sessionStorage ๊ฐ์ฒด
storage ์ด๋ฒคํธ๋ ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํจ ์คํ ๋ฆฌ์ง๋ฅผ ์ ์ธํ๊ณ ์คํ ๋ฆฌ์ง์์ ์ ๊ทผ ๊ฐ๋ฅํ window ๊ฐ์ฒด ์ ๋ถ์์ ์ผ์ด๋ฉ๋๋ค.
- ๋ ๊ฐ์ ์ฐฝ์ ๊ฐ์ ์ฌ์ดํธ๋ฅผ ๋์๋๊ณ Local Storage ๋ฐ์ดํฐ ๋ณ๊ฒฝ
- ๋ ์ฐฝ์์ ๋ชจ๋ storage ์ด๋ฒคํธ๋ฅผ ์์ ํ๊ณ ์๊ธฐ ๋๋ฌธ์ ํ ์ฐฝ์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐฑ์ ํ๋ฉด ๋ค๋ฅธ ์ฐฝ์์ ํด๋น ์ฌํญ์ด ๋ฐ์๋๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
window.addEventListener('storage', function (e) {
if (e.key === 'myData') {
console.log('Key: ' + e.key);
console.log('Old Value: ' + e.oldValue);
console.log('New Value: ' + e.newValue);
console.log('Storage Area: ' + e.storageArea);
console.log('URL: ' + e.url);
}
});
๐ฉ ์ฐธ๊ณ
- https://ko.javascript.info/localstorage#ref-785
- https://www.zerocho.com/category/HTML&DOM/post/5918515b1ed39f00182d3048
- https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage