{"id":5341,"date":"2019-12-17T16:59:31","date_gmt":"2019-12-17T07:59:31","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=5341"},"modified":"2019-12-17T16:59:31","modified_gmt":"2019-12-17T07:59:31","slug":"localstorage%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9%e3%83%bb%e5%ae%9f%e4%be%8b%e5%85%a8%ef%bc%99%e4%bb%b6%ef%bc%81","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=5341","title":{"rendered":"localStorage\u306e\u4f7f\u3044\u65b9\u30fb\u5b9f\u4f8b\u5168\uff19\u4ef6\uff01"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u3053\u306e\u9593\u516c\u958b\u3057\u305f<a href=\"https:\/\/blog.capilano-fw.com\/?p=5305\">\u30e6\u30fc\u30b6\u30d3\u30ea\u30c6\u30a3\uff01\u30da\u30fc\u30b8\u79fb\u52d5\u3057\u3066\u3082Vue\u306e\u72b6\u614b\u3092\u4fdd\u6301\u3059\u308b\u65b9\u6cd5<\/a>\u3067\u5c11\u3057\u89e6\u308c\u307e\u3057\u305f\u304c<code>JavaScript<\/code>\u306b\u306f<code>Cookie<\/code>\u306e\u3088\u3046\u306b\u30d6\u30e9\u30a6\u30b6\u5185\u306b\u30c7\u30fc\u30bf\u3092\u4fdd\u7ba1\u3057\u3066\u304a\u304f\u6a5f\u80fd\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u4f8b\u3048\u3070\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\u306a\u3069\u3092\u4fdd\u5b58\u3057\u3066\u304a\u3051\u3070\u3001\u305d\u306e\u30da\u30fc\u30b8\u306b\u623b\u3063\u3066\u304d\u305f\u3068\u304d\u306b\u691c\u7d22\u6e08\u307f\u306e\u30da\u30fc\u30b8\u3092\u898b\u305b\u305f\u308a\u3001\u591c\u9593\u30e2\u30fc\u30c9\u306e\u8a2d\u5b9a\u306a\u3069\u3092\u3057\u3066\u304a\u3044\u3066\u898b\u305f\u76ee\u3092\u5909\u66f4\u3057\u305f\u308a\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3068\u3044\u3046\u308f\u3051\u3067\u3059\u3002<\/p>\n<p>\u3064\u307e\u308a\u3001<code>localStorage<\/code>\u3084<code>sessionStorage<\/code>\u3068\u547c\u3070\u308c\u308b\u6a5f\u80fd\u3067\u3059\u306d\u3002<\/p>\n<p>\u305d\u3053\u3067\u4eca\u56de\u306f\u3053\u306e\u6a5f\u80fd\u306e\u4f7f\u3044\u65b9\u3092\u5b9f\u4f8b\u3068\u3057\u3066\u3054\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u305c\u3072\u7686\u3055\u3093\u306e\u304a\u5f79\u306b\u7acb\u3066\u308b\u3068\u5b09\u3057\u3044\u3067\u3059\ud83d\ude0a\u2728<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2173 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/11\/no_24.png\" alt=\"\" width=\"300\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/11\/no_24.png 300w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/11\/no_24-281x300.png 281w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/p>\n<p><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> Google Chrome 78<\/p>\n<p>\u203b \u306a\u304a\u3001\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\u306f\u540c\u3058\u306a\u306e\u3067<code>localStorage<\/code>\u3092\u4f7f\u3063\u3066\u8aac\u660e\u3092\u3057\u307e\u3059\u3002<code>localStorage<\/code>\u3068<code>sessionStorage<\/code>\u306e\u9055\u3044\u306f<a href=\"#local_storage_n_session_storage\">localStorage\u3068sessionStorage\u306e\u9055\u3044\u3068\u306f\uff1f\uff1f<\/a>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<h1>\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9<\/h1>\n<h2 id=\"set_item\">\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b<\/h2>\n<p>\u30c7\u30fc\u30bf\u306e\u4fdd\u5b58\u306f<code>setItem()<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre><strong>localStorage.setItem('test_key', '\u30c6\u30b9\u30c8\u6587\u5b57\u5217');<\/strong><\/pre>\n<p>\u203b \u305f\u3060\u3057\u3001<strong>\u4fdd\u5b58\u3055\u308c\u305f\u5024\u306f\u6587\u5b57\u5217\u306b\u306a\u308a\u307e\u3059<\/strong>\u306e\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u6570\u5024\u3092\u4fdd\u5b58\u3057\u3066\u3082\u53d6\u308a\u51fa\u3059\u3068\u304d\u306b\u306f\u6587\u5b57\u5217\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u306e\u3067\u6ce8\u610f\u304c\u5fc5\u8981\u3067\u3059\u3002<\/p>\n<pre><strong>localStorage.setItem('number', 1);<\/strong> \/\/ \u6587\u5b57\u5217\u3068\u3057\u3066\u4fdd\u5b58\u3055\u308c\u308b<\/pre>\n<p>\u203b \u306a\u304a\u3001\u914d\u5217\u3084\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u4fdd\u5b58\u3059\u308b\u5834\u5408\u306f<a href=\"#array_object\">\u914d\u5217\u3084\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4fdd\u5b58\u3001\u53d6\u5f97\u3059\u308b<\/a>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<h2>\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u30c7\u30fc\u30bf\u306e\u53d6\u5f97\u306f\u3001<code>getItem()<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre><strong>const value = localStorage.getItem('test_key'); \/\/ \u6587\u5b57\u5217<\/strong><\/pre>\n<p>\u203b <a href=\"#set_item\">\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b<\/a>\u3067\u3082\u8aac\u660e\u3057\u305f\u3068\u304a\u308a\u3001\u53d6\u5f97\u3067\u304d\u308b\u5024\u306f\u6587\u5b57\u5217\u306b\u306a\u308a\u307e\u3059\u3002<br \/>\n<span style=\"font-size: 1rem;\">\u203b \u307e\u305f\u3001\u914d\u5217\u3084\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u53d6\u5f97\u3059\u308b\u5834\u5408\u306f<\/span><a style=\"font-size: 1rem;\" href=\"#array_object\">\u914d\u5217\u3084\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4fdd\u5b58\u3001\u53d6\u5f97\u3059\u308b<\/a><span style=\"font-size: 1rem;\">\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/span><\/p>\n<h2>\u7279\u5b9a\u306e\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b<\/h2>\n<p>\u3042\u308b\u7279\u5b9a\u306e\u30ad\u30fc\u3092\u4f7f\u3063\u3066\u3001\u305d\u306e\u5024\u3092\u524a\u9664\u3059\u308b\u306b\u306f<code>removeItem()<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre><strong>localStorage.removeItem('test_key');<\/strong> \/\/ test_key\u306e\u5185\u5bb9\u3092\u524a\u9664<\/pre>\n<h2>\u5168\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b<\/h2>\n<p><code>localStorage<\/code>\u306e\u30c7\u30fc\u30bf\u3092\u524a\u9664\u3059\u308b\u306b\u306f<code>clear()<\/code>\u3092\u4f7f\u3044\u307e\u3059\u3002<\/p>\n<pre><strong>localStorage.clear();<\/strong><\/pre>\n<p>\u203b \u305f\u3060\u3057\u3001\u524a\u9664\u3055\u308c\u308b\u306e\u306f\u300c\u30c9\u30e1\u30a4\u30f3\u5185\u306e\u300d\u5168\u30c7\u30fc\u30bf\u3067\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u30b5\u30d6\u30c9\u30e1\u30a4\u30f3\u3084\u30dd\u30fc\u30c8\u304c\u9055\u3046\u30c7\u30fc\u30bf\u304c\u524a\u9664\u3055\u308c\u308b\u3053\u3068\u306f\u3042\u308a\u307e\u305b\u3093\u3002\u8a73\u3057\u304f\u306f\u3001<a href=\"#available_range\">localStorage\u304c\u6709\u52b9\u306a\u7bc4\u56f2\u306f\uff1f\uff1f<\/a>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<h1>\u5fdc\u7528\u7684\u306a\u4f7f\u3044\u65b9<\/h1>\n<h2 id=\"array_object\">\u914d\u5217\u3084\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4fdd\u5b58\u3001\u53d6\u5f97\u3059\u308b<\/h2>\n<p><code>localStorage<\/code>\u304c\u4fdd\u5b58\u3067\u304d\u308b\u306e\u306f\u6587\u5b57\u5217\u3060\u3051\u3067\u3059\u306e\u3067\u3001\u3082\u3057\u914d\u5217\u3084\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4fdd\u5b58\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b<code>JSON<\/code>\u306b\u5909\u63db\u3057\u3066\u304b\u3089\u4fdd\u5b58\u3057\u3001\u3055\u3089\u306b\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u5834\u5408\u306b\u306f<code>JSON<\/code>\u3092\u5143\u306b\u623b\u3059\u5fc5\u8981\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>const data = {<\/strong>\r\n<strong>    key1: 'value1',<\/strong>\r\n<strong>    key2: 'value2',<\/strong>\r\n<strong>    key3: 'value3',<\/strong>\r\n<strong>};<\/strong>\r\n<strong>localStorage.setItem('test_key', JSON.stringify(data)); \/\/ JSON\u306b\u5909\u63db<\/strong>\r\n<strong>const values = JSON.parse(localStorage.getItem('test_key')); \/\/ \u5143\u306b\u623b\u3059<\/strong><\/pre>\n<h2>\u5168\u30c7\u30fc\u30bf\u3092\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3068\u3057\u3066\u53d6\u5f97\u3059\u308b<\/h2>\n<p><code>localStorage<\/code>\u306b\u4fdd\u5b58\u3055\u308c\u308b\u30c7\u30fc\u30bf\u5168\u3066\u3092\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u5f62\u5f0f\u3067\u53d6\u5f97\u3059\u308b\u65b9\u6cd5\u3067\u3059\u3002<\/p>\n<pre><strong>let object = {};<\/strong>\r\n\r\n<strong>for(let i = 0 ; i &lt; localStorage.length ; i++) {<\/strong>\r\n\r\n<strong>    let key = localStorage.key(i);<\/strong>\r\n<strong>    object[key] = localStorage.getItem(key);<\/strong>\r\n\r\n<strong>}<\/strong><\/pre>\n<p>\u306a\u304a\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b<code>for in<\/code> \u5f62\u5f0f\u3067\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3068<code>length<\/code>\u306a\u3069\u3082\u5024\u306b\u5165\u3063\u3066\u304f\u308b\u306e\u3067\u4f7f\u3046\u3079\u304d\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<pre>\/\/ <strong>\u3010\u6ce8\u610f\u3011\u3053\u308c\u306f\u9593\u9055\u3063\u305f\u4f8b\u3067\u3059<\/strong>\r\nlet object = {};\r\n\r\nfor(let key in localStorage) {\r\n\r\n    object[key] = localStorage.getItem(key);\r\n\r\n}<\/pre>\n<h2>\u5168\u30c7\u30fc\u30bf\u3092\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3068\u3057\u3066\u53d6\u5f97\u3059\u308b<\/h2>\n<p>\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u304c\u914d\u5217\u3067\u307e\u3068\u3081\u3089\u308c\u305f\u3001\u3044\u308f\u3086\u308b\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3067\u53d6\u5f97\u3059\u308b\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<pre><strong>const collection = Object.keys(localStorage).map(key =&gt; {<\/strong>\r\n\r\n<strong>    return {<\/strong>\r\n<strong>        key: key,<\/strong>\r\n<strong>        value: localStorage.getItem(key)<\/strong>\r\n<strong>    }<\/strong>\r\n\r\n<strong>});<\/strong><\/pre>\n<p>\u53d6\u5f97\u3067\u304d\u308b\u5185\u5bb9\u306f\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5343\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/12\/local_storage_1.png\" alt=\"\" width=\"255\" height=\"52\" \/><\/p>\n<p>\u306a\u304a\u3001\u30ad\u30fc\u3067\u4e26\u3079\u66ff\u3048\u305f\u3044\u5834\u5408\u306f<code>sort()<\/code>\u3092\u8ffd\u52a0\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>const collection = Object.keys(localStorage).<strong>sort().<\/strong>map(key =&gt; {\r\n\r\n    return {\r\n        key: key,\r\n        value: localStorage.getItem(key)\r\n    }\r\n\r\n});<\/pre>\n<p>\u7d50\u679c\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5347\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/12\/local_storage_2.png\" alt=\"\" width=\"251\" height=\"51\" \/><\/p>\n<h2>localStorage\u304c\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u3066\u3044\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3059\u308b<\/h2>\n<p><code>localStorage<\/code>\u306f\u3001<code>IE<\/code>\u3092\u542b\u3080\u591a\u304f\u306e\u30d6\u30e9\u30a6\u30b6\u3067\u30b5\u30dd\u30fc\u30c8\u3055\u308c\u3066\u3044\u307e\u3059\u304c\u30012019.12.16\u65e5\u73fe\u5728\u3001<code>opera mini<\/code>\u306a\u3069\u3067\u306f\u30b5\u30dd\u30fc\u30c8\u5916\u3068\u306a\u3063\u3066\u3044\u307e\u3059\u3002\u305d\u306e\u305f\u3081\u3001\u3082\u3057<code>localStorage<\/code>\u306e\u30b5\u30dd\u30fc\u30c8\u3092\u30c1\u30a7\u30c3\u30af\u3057\u305f\u3044\u5834\u5408\u306f\u6b21\u306e\u3088\u3046\u306b\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>if(typeof localStorage === 'undefined') {<\/strong>\r\n\r\n<strong>    console.log('\u3053\u306e\u30d6\u30e9\u30a6\u30b6\u306f\u3001localStorage \u3092\u30b5\u30dd\u30fc\u30c8\u3057\u3066\u3044\u307e\u305b\u3093\u3002');<\/strong>\r\n\r\n<strong>}<\/strong><\/pre>\n<p>\u203b \u306a\u304a\u3001\u8a73\u3057\u3044\u30b5\u30dd\u30fc\u30c8\u72b6\u6cc1\u306f\u4ee5\u4e0b\u306e\u30da\u30fc\u30b8\u3067\u78ba\u8a8d\u3067\u304d\u307e\u3059\u3002<\/p>\n<ul>\n<li><a href=\"https:\/\/caniuse.com\/#search=localstorage\" target=\"_blank\" rel=\"noopener noreferrer\">localStorage<\/a><\/li>\n<li><a href=\"https:\/\/caniuse.com\/#search=sessionstorage\" target=\"_blank\" rel=\"noopener noreferrer\">sessionStorage<\/a><\/li>\n<\/ul>\n<h2>\u7279\u5b9a\u306e\u30ad\u30fc\u304c\u5b58\u5728\u3057\u3066\u3044\u308b\u304b\u30c1\u30a7\u30c3\u30af\u3059\u308b<\/h2>\n<p>\u3059\u3067\u306b<code>localStorage<\/code>\u306b\u30c7\u30fc\u30bf\u304c\u4fdd\u5b58\u3055\u308c\u3066\u3044\u308b\u304b\u3069\u3046\u304b\u3092\u30c1\u30a7\u30c3\u30af\u3059\u308b\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b<code>hasOwnProperty<\/code>\u3092\u4f7f\u3063\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>if(localStorage.hasOwnProperty('test_key')) {<\/strong>\r\n\r\n<strong>    console.log('\u3053\u306e\u30ad\u30fc\u306f\u5b58\u5728\u3057\u3066\u3044\u307e\u3059');<\/strong>\r\n\r\n<strong>}<\/strong><\/pre>\n<p>\u307e\u305f\u3001<code>localStorage<\/code>\u306f\u4fdd\u5b58\u3055\u308c\u305f\u30c7\u30fc\u30bf\u304c\u306a\u3044\u72b6\u614b\u3067<code>getItem()<\/code>\u3092\u3059\u308b\u3068<code>null<\/code>\u304c\u8fd4\u3063\u3066\u304f\u308b\u306e\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u3082\u3044\u3044\u3067\u3057\u3087\u3046\u3002\uff08\u305f\u3060\u3057<code>null<\/code>\u3092\u4fdd\u5b58\u3059\u308b\u3068\u3001\u6587\u5b57\u5217\u306e<code>\"null\"<\/code>\u304c\u4fdd\u5b58\u3055\u308c\u308b\u306e\u3067\u6bd4\u8f03\u6f14\u7b97\u5b50\u306f\u578b\u3082\u542b\u3081\u305f\u53b3\u683c\u306a\u5f62\u5f0f\u3092\u4f7f\u3046\u3053\u3068\u3092\u304a\u3059\u3059\u3081\u3057\u307e\u3059\uff09<\/p>\n<pre><strong>if(localStorage.getItem('test_key') !== null) {<\/strong>\r\n\r\n<strong>    console.log('\u3053\u306e\u30ad\u30fc\u306f\u5b58\u5728\u3057\u3066\u3044\u307e\u3059');<\/strong>\r\n\r\n<strong>}<\/strong><\/pre>\n<h1 id=\"available_range\">localStorage\u304c\u6709\u52b9\u306a\u7bc4\u56f2\u306f\uff1f<\/h1>\n<p>\u540c\u3058\u30c9\u30e1\u30a4\u30f3\u5185\u3060\u3051\u3067\u6709\u52b9\u3067\u3059\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u3082\u3057<code>example.com<\/code>\u3067\u4fdd\u5b58\u3057\u305f\u30c7\u30fc\u30bf\u3092<code>sub.example.com<\/code>\u306a\u3069\u306e\u30b5\u30d6\u30c9\u30e1\u30a4\u30f3\u304b\u3089\u547c\u3073\u51fa\u3059\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002<\/p>\n<p>\u307e\u305f\u3001\u30dd\u30fc\u30c8\u756a\u53f7\u304c\u9055\u3063\u3066\u3044\u3066\u3082<code>localStorage<\/code>\u306f\u5225\u7ba1\u7406\u3055\u308c\u307e\u3059\u3002\u4f8b\u3048\u3070\u3001<code>example.com<\/code>\u3067\u4fdd\u5b58\u3057\u305f\u30c7\u30fc\u30bf\u3092\u3001<code>example.com:8000<\/code>\u304b\u3089\u53d6\u5f97\u3059\u308b\u3053\u3068\u306f\u3067\u304d\u307e\u305b\u3093\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u3082\u3057\u30b5\u30d6\u30c9\u30e1\u30a4\u30f3\u3084\u30dd\u30fc\u30c8\u9055\u3044\u306e\u30b5\u30a4\u30c8\u304b\u3089\u3067\u3082<code>localStorage<\/code>\u306b\u30a2\u30af\u30bb\u30b9\u3092\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u89aa\u30b5\u30a4\u30c8\u306e\u4e2d\u3067\u30da\u30fc\u30b8\u4e0a\u306b\u8868\u793a\u3055\u308c\u306a\u3044<code>iframe<\/code>\u3092\u3064\u304f\u308a\u3001\u305d\u306e\u5148\u3067<code>localStorage<\/code>\u3078\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u306a\u3069\u306e\u30b3\u30fc\u30c9\u304c\u5fc5\u8981\u306b\u306a\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n<p>\u5b9f\u969b\u306e\u30b3\u30fc\u30c9\u306f<a href=\"#share_local_storage\">\u304a\u307e\u3051\uff1a\u4ed6\u306e\u30b5\u30a4\u30c8\u3068localStorage\u3092\u5171\u6709\u3059\u308b<\/a>\u3092\u3054\u89a7\u304f\u3060\u3055\u3044\u3002<\/p>\n<h1>\u304a\u307e\u3051\uff1a\u4ed6\u306e\u30b5\u30a4\u30c8\u3068localStorage\u3092\u5171\u6709\u3059\u308b<\/h1>\n<p><code>localStorage<\/code>\u3092\u4ed6\u306e\u30b5\u30a4\u30c8\u3068\u5171\u6709\u3059\u308b\u306b\u306f\u3001\u72ec\u81ea\u306b\u30c7\u30fc\u30bf\u3092\u4ed6\u306e\u30b5\u30a4\u30c8\u306b\u9001\u4fe1\u3057\u3001\u3055\u3089\u306b\u9001\u4fe1\u5148\u3067<code>localStorage<\/code>\u306b\u4fdd\u5b58\u3059\u308b\u30b3\u30fc\u30c9\u304c\u5fc5\u8981\u306b\u306a\u3063\u3066\u304d\u307e\u3059\u3002<\/p>\n<p>\u30b7\u30f3\u30d7\u30eb\u306a\u4f8b\u306f\u6b21\u306e\u3068\u304a\u308a\u3067\u3059\u3002<\/p>\n<p><strong>\uff08\u30e1\u30a4\u30f3\u306e\u30b5\u30a4\u30c8\uff09<\/strong><\/p>\n<pre>&lt;html&gt;\r\n&lt;body&gt;\r\n    &lt;script&gt;\r\n\r\n        <strong>window.onload = () =&gt; {<\/strong>\r\n\r\n<strong>            localStorage.setItem('key_1', 'to_external_1');<\/strong>\r\n<strong>            localStorage.setItem('key_2', 'to_external_2');<\/strong>\r\n<strong>            localStorage.setItem('key_3', 'to_external_3');<\/strong>\r\n\r\n<strong>            \/\/ localStorage\u306e\u5168\u30c7\u30fc\u30bf\u3092\u53d6\u5f97<\/strong>\r\n<strong>            let data = {};<\/strong>\r\n\r\n<strong>            for(let i = 0 ; i &lt; localStorage.length ; i++) {<\/strong>\r\n\r\n<strong>                let key = localStorage.key(i);<\/strong>\r\n<strong>                data[key] = localStorage.getItem(key);<\/strong>\r\n\r\n<strong>            }<\/strong>\r\n\r\n<strong>            \/\/ JSON + URL\u30a8\u30f3\u30b3\u30fc\u30c9<\/strong>\r\n<strong>            const json = JSON.stringify(data);<\/strong>\r\n<strong>            const url = 'http:\/\/\uff08\u5916\u90e8\u30b5\u30a4\u30c8\u306e\u30c9\u30e1\u30a4\u30f3\uff09\/share_local_storage?'+ encodeURIComponent(json);<\/strong>\r\n\r\n<strong>            \/\/ iframe\u3067\u5225\u30b5\u30a4\u30c8\u306b\u30c7\u30fc\u30bf\u3092\u9001\u4fe1<\/strong>\r\n<strong>            let iframe = document.createElement('iframe');<\/strong>\r\n<strong>            iframe.style.display = 'none';<\/strong>\r\n<strong>            iframe.src = url;<\/strong>\r\n<strong>            document.body.appendChild(iframe);<\/strong>\r\n\r\n<strong>        }<\/strong>\r\n\r\n    &lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>\u3053\u3053\u3067\u306f\u3001<code>localStorage<\/code>\u306e\u5168\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3066<code>JSON<\/code> + URL\u30a8\u30f3\u30b3\u30fc\u30c9\u5316\u3057\u3066\u3001\u305d\u306e\u5024\u3092\u30d1\u30e9\u30e1\u30fc\u30bf\u3068\u3057\u305fURL\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002\uff08\u3064\u307e\u308a<code>localStorage<\/code>\u306e\u30c7\u30fc\u30bf\u3092\u9001\u4fe1\u3057\u307e\u3059\uff09<\/p>\n<p><strong>\uff08\u5916\u90e8\u306e\u30b5\u30a4\u30c8\uff09<\/strong><\/p>\n<p>\u305d\u3057\u3066\u3001\u5916\u90e8\u306e\u30b5\u30a4\u30c8\u3067\u306f\u6b21\u306e\u3088\u3046\u306b\u3057\u3066<code>localStorage<\/code>\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3057\u3001\u518d\u5ea6\u305d\u306e\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58\u3059\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>&lt;html&gt;\r\n&lt;body&gt;\r\n&lt;script&gt;\r\n\r\n<strong>    window.onload = () =&gt; {<\/strong>\r\n\r\n<strong>        const json = window.location.search.substr(1);<\/strong>\r\n<strong>        const data = JSON.parse(decodeURIComponent(json));<\/strong>\r\n\r\n<strong>        for(let key in data) {<\/strong>\r\n\r\n<strong>            \/\/ \u9001\u4fe1\u3055\u308c\u305f\u30c7\u30fc\u30bf\u3092\u4fdd\u5b58<\/strong>\r\n<strong>            localStorage.setItem(key, data[key]);<\/strong>\r\n\r\n<strong>        }<\/strong>\r\n\r\n<strong>    };<\/strong>\r\n\r\n&lt;\/script&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>\u3082\u3057\u53cc\u65b9\u5411\u3067\u30c7\u30fc\u30bf\u5171\u6709\u3057\u305f\u3044\u5834\u5408\u306f\u3001\u540c\u3058\u3088\u3046\u306b\u9006\u65b9\u5411\u306e\u30b3\u30fc\u30c9\u3082\u5b9f\u88c5\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<h1 id=\"local_storage_n_session_storage\">localStorage\u3068sessionStroage\u306e\u9055\u3044\u3068\u306f\uff1f\uff1f<\/h1>\n<p><code>sessionStorage<\/code>\u3082\u3053\u3053\u307e\u3067\u3067\u8aac\u660e\u3057\u3066\u304d\u305f<code>localStorage<\/code>\u3068\u307b\u307c\u540c\u3058\u3067\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u30c7\u30fc\u30bf\u306e\u4fdd\u5b58\u3084\u53d6\u5f97\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre><strong>sessionStorage.setItem('test_key', '\u30c6\u30b9\u30c8\u3067\u3059');<\/strong>\r\n<strong>const value = sessionStorage.getItem('test_key');<\/strong>\r\n\r\n<strong>console.log(value); \/\/ \u30c6\u30b9\u30c8\u3067\u3059<\/strong><\/pre>\n<p>\u305f\u3060\u3057\u3001\u3053\u306e\uff12\u3064\u306f\u30c7\u30fc\u30bf\u306e\u4fdd\u5b58\u671f\u9593\u304c\u9055\u3044\u307e\u3059\u3002<code>sessionStorage<\/code>\u306e\u65b9\u306f\u30bf\u30d6\u3084\u30a6\u30a3\u30f3\u30c9\u30a6\u304c\u9589\u3058\u3089\u308c\u308b\u3068\u6d88\u53bb\u3055\u308c\u307e\u3059\u304c\u3001<code>localStorage<\/code>\u306f\u30d6\u30e9\u30a6\u30b6\u304c\u524a\u9664\u3055\u308c\u306a\u3044\u9650\u308a\u3001\u6c38\u9060\u306b\u30c7\u30fc\u30bf\u304c\u6b8b\u308b\u3053\u3068\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u3001\u4eca\u56de\u306f<code>localStorage<\/code>\u3068<code>sessionStorage<\/code>\u306e\u4f7f\u3044\u65b9\u3092\u5b9f\u4f8b\u3067\u3054\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u3053\u308c\u3089\u306e\u6a5f\u80fd\u3092\u4f7f\u3046\u76ee\u5b89\u306f\u3001\u300c\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u306b\u4fdd\u5b58\u3059\u308b\u307b\u3069\u3058\u3083\u306a\u3044\u3051\u3069\u5f8c\u3067\u53d6\u308a\u51fa\u3057\u305f\u3044\u30c7\u30fc\u30bf\u300d\u3068\u3044\u3046\u3053\u3068\u306b\u306a\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002\uff08\u500b\u4eba\u7684\u306b\u601d\u3044\u3064\u304f\u306e\u306f\u691c\u7d22\u30ad\u30fc\u30ef\u30fc\u30c9\u3084\u7d5e\u308a\u8fbc\u307f\u306e\u8a2d\u5b9a\u306a\u3069\u3067\u3057\u3087\u3046\u304b\uff09<\/p>\n<p>\u305c\u3072\u7686\u3055\u3093\u3082\u6d3b\u7528\u3057\u3066\u30b5\u30a4\u30c8\u958b\u767a\u306b\u5f79\u7acb\u3066\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u306d\u3002<\/p>\n<p>\u3067\u306f\u3067\u306f\u301c\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-418 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/35.png\" alt=\"\" width=\"370\" height=\"240\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/35.png 370w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/04\/35-300x195.png 300w\" sizes=\"auto, (max-width: 370px) 100vw, 370px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u3053\u306e\u9593\u516c\u958b\u3057\u305f\u30e6\u30fc\u30b6\u30d3\u30ea\u30c6\u30a3\uff01\u30da\u30fc\u30b8\u79fb\u52d5\u3057\u3066\u3082Vue\u306e\u72b6\u614b\u3092\u4fdd\u6301\u3059\u308b\u65b9\u6cd5\u3067\u5c11\u3057\u89e6\u308c\u307e\u3057\u305f\u304cJavaScript\u306b\u306fCookie\u306e\u3088\u3046\u306b\u30d6\u30e9\u30a6\u30b6\u5185\u306b\u30c7\u30fc &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=5341\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;localStorage\u306e\u4f7f\u3044\u65b9\u30fb\u5b9f\u4f8b\u5168\uff19\u4ef6\uff01&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":5348,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-5341","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/5341","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5341"}],"version-history":[{"count":6,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/5341\/revisions"}],"predecessor-version":[{"id":5350,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/5341\/revisions\/5350"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/5348"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5341"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5341"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5341"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}