{"id":772,"date":"2021-03-08T22:09:22","date_gmt":"2021-03-08T14:09:22","guid":{"rendered":"https:\/\/www.shineskr.com\/?p=772"},"modified":"2021-10-17T00:16:28","modified_gmt":"2021-10-16T16:16:28","slug":"cookie%e3%80%81localstorage-%e4%b8%8e-sessionstorage%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/www.shineskr.com\/772.html","title":{"rendered":"Cookie\u3001LocalStorage \u4e0e SessionStorage\u7684\u533a\u522b"},"content":{"rendered":"<!--wp-compress-html--><!--wp-compress-html no compression-->\n<h2 class=\"wp-block-heading\">\u57fa\u672c\u6982\u5ff5<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><a href=\"https:\/\/www.shineskr.com\/tag\/cookie\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e Cookie \u7684\u6587\u7ae0\" target=\"_blank\">Cookie<\/a><\/strong><\/h4>\n\n\n\n<p><a href=\"https:\/\/www.shineskr.com\/tag\/cookie\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e Cookie \u7684\u6587\u7ae0\" target=\"_blank\">Cookie<\/a>\u662f\u670d\u52a1\u5668\u53d1\u9001\u5230\u6d4f\u89c8\u5668\u7684\u4e00\u5c0f\u6bb5\u6570\u636e\uff0c\u4f1a\u5728\u6d4f\u89c8\u5668\u4e0b\u6b21\u5411\u540c\u4e00\u670d\u52a1\u5668\u518d\u53d1\u8d77\u8bf7\u6c42\u65f6\u88ab\u643a\u5e26\u5e76\u53d1\u9001\u5230\u670d\u52a1\u5668\u4e0a\u3002<\/p>\n\n\n\n<p><a href=\"https:\/\/www.shineskr.com\/tag\/cookie\" class=\"tag-tooltip\" data-toggle=\"tooltip\" title=\"\u67e5\u770b\u66f4\u591a\u5173\u4e8e Cookie \u7684\u6587\u7ae0\" target=\"_blank\">Cookie<\/a>\u8bde\u751f\u4e4b\u521d\u7684\u4f5c\u7528\u5c31\u662f\u89e3\u51b3HTTP\u7684\u65e0\u72b6\u6001\u8bf7\u6c42\uff0c\u7528\u6765\u8bb0\u5f55\u4e00\u4e9b\u7528\u6237\u76f8\u5173\u7684\u4e00\u4e9b\u72b6\u6001\u3002<\/p>\n\n\n\n<ul><li>\u4f1a\u8bdd\u72b6\u6001\u7ba1\u7406\uff08\u5982\u7528\u6237\u767b\u5f55\u72b6\u6001\u3001\u8d2d\u7269\u8f66\u3001\u6e38\u620f\u5206\u6570\u6216\u5176\u5b83\u9700\u8981\u8bb0\u5f55\u7684\u4fe1\u606f\uff09<\/li><li>\u4e2a\u6027\u5316\u8bbe\u7f6e\uff08\u5982\u7528\u6237\u81ea\u5b9a\u4e49\u8bbe\u7f6e\u3001\u4e3b\u9898\u7b49\uff09<\/li><li>\u6d4f\u89c8\u5668\u884c\u4e3a\u8ddf\u8e2a\uff08\u5982\u8ddf\u8e2a\u5206\u6790\u7528\u6237\u884c\u4e3a\u7b49\uff09<\/li><\/ul>\n\n\n\n<p>\u56e0\u4e3a\u4e00\u4e9b\u524d\u7aef\u4ea4\u4e92\u7684\u9700\u8981\uff0c\u540e\u6765cookie\u4e5f\u88ab\u7528\u4e8e\u5b58\u50a8\u4e00\u4e9b\u5ba2\u6237\u7aef\u7684\u6570\u636e\u3002 Cookie\u7684\u539f\u751fapi\u4e0d\u53cb\u597d\uff0c\u9700\u8981\u81ea\u884c\u5c01\u88c5\u4e00\u4e0b\u3002 <\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"localstorage\"><strong>localStorage<\/strong><\/h4>\n\n\n\n<p> HTML5 \u63d0\u4f9b\u4e86\u4e24\u79cd\u5728\u5ba2\u6237\u7aef\u5b58\u50a8\u6570\u636e\u7684\u65b0\u65b9\u6cd5\uff1alocalStorage\u548csessionStorage\uff0c\u6302\u8f7d\u5728window\u5bf9\u8c61\u4e0b\u3002<\/p>\n\n\n\n<p>webStorage\u662f\u672c\u5730\u5b58\u50a8\uff0c\u6570\u636e\u4e0d\u662f\u7531\u670d\u52a1\u5668\u8bf7\u6c42\u4f20\u9012\u7684\u3002\u4ece\u800c\u5b83\u53ef\u4ee5\u5b58\u50a8\u5927\u91cf\u7684\u6570\u636e\uff0c\u800c\u4e0d\u5f71\u54cd\u7f51\u7ad9\u7684\u6027\u80fd\u3002<\/p>\n\n\n\n<p>WebStorage\u7684\u76ee\u7684\u662f\u4e3a\u4e86\u514b\u670d\u7531cookie\u5e26\u6765\u7684\u4e00\u4e9b\u9650\u5236\uff0c\u5f53\u6570\u636e\u9700\u8981\u88ab\u4e25\u683c\u63a7\u5236\u5728\u5ba2\u6237\u7aef\u4e0a\u65f6\uff0c\u65e0\u987b\u6301\u7eed\u5730\u5c06\u6570\u636e\u53d1\u56de\u670d\u52a1\u5668\u3002\u6bd4\u5982\u5ba2\u6237\u7aef\u9700\u8981\u4fdd\u5b58\u7684\u4e00\u4e9b\u7528\u6237\u884c\u4e3a\u6216\u6570\u636e\uff0c\u6216\u4ece\u63a5\u53e3\u83b7\u53d6\u7684\u4e00\u4e9b\u77ed\u671f\u5185\u4e0d\u4f1a\u66f4\u65b0\u7684\u6570\u636e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u5229\u7528Web Storage\u6765\u5b58\u50a8\u3002 localStorage\u7684\u751f\u547d\u5468\u671f\u662f\u6c38\u4e45\u6027\u7684\u3002localStorage\u5b58\u50a8\u7684\u6570\u636e\uff0c\u5373\u4f7f\u5173\u95ed\u6d4f\u89c8\u5668\uff0c\u4e5f\u4e0d\u4f1a\u8ba9\u6570\u636e\u6d88\u5931\uff0c\u9664\u975e\u4e3b\u52a8\u7684\u53bb\u5220\u9664\u6570\u636e\u3002\u5982\u679c\u60f3\u8bbe\u7f6e\u5931\u6548\u65f6\u95f4\uff0c\u9700\u81ea\u884c\u5c01\u88c5\u3002 <\/p>\n\n\n\n<p>\u65e9\u5728 IE 6 \u65f6\u4ee3\uff0c\u5c31\u6709\u4e00\u4e2a\u53eb userData \u7684\u4e1c\u897f\u7528\u4e8e\u672c\u5730\u5b58\u50a8\uff0c\u800c\u5f53\u65f6\u8003\u8651\u5230\u6d4f\u89c8\u5668\u517c\u5bb9\u6027\uff0c\u66f4\u901a\u7528\u7684\u65b9\u6848\u662f\u4f7f\u7528 Flash\u3002\u800c\u5982\u4eca\uff0clocalStorage \u88ab\u5927\u591a\u6570\u6d4f\u89c8\u5668\u6240\u652f\u6301\uff0c\u5982\u679c\u4f60\u7684\u7f51\u7ad9\u9700\u8981\u652f\u6301 IE6+\uff0c\u90a3\u4ee5 userData \u4f5c\u4e3a\u4f60\u7684 polyfill \u7684\u65b9\u6848\u662f\u79cd\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"sessionstorage\"><strong>sessionStorage<\/strong><\/h4>\n\n\n\n<p>sessionStorage \u4e0e localStorage \u7684\u63a5\u53e3\u7c7b\u4f3c\uff0c\u4f46\u4fdd\u5b58\u6570\u636e\u7684\u751f\u547d\u5468\u671f\u4e0e localStorage \u4e0d\u540c\u3002\u505a\u8fc7\u540e\u7aef\u5f00\u53d1\u7684\u540c\u5b66\u5e94\u8be5\u77e5\u9053 Session \u8fd9\u4e2a\u8bcd\u7684\u610f\u601d\uff0c\u76f4\u8bd1\u8fc7\u6765\u662f\u201c\u4f1a\u8bdd\u201d\u3002\u800c sessionStorage \u662f\u4e00\u4e2a\u524d\u7aef\u7684\u6982\u5ff5\uff0c\u5b83\u53ea\u662f\u53ef\u4ee5\u5c06\u4e00\u90e8\u5206\u6570\u636e\u5728\u5f53\u524d\u4f1a\u8bdd\u4e2d\u4fdd\u5b58\u4e0b\u6765\uff0c\u5237\u65b0\u9875\u9762\u6570\u636e\u4f9d\u65e7\u5b58\u5728\u3002\u4f46\u5f53\u9875\u9762\u5173\u95ed\u540e\uff0csessionStorage \u4e2d\u7684\u6570\u636e\u5c31\u4f1a\u88ab\u6e05\u7a7a\u3002\u53ea\u6709\u5728\u5f53\u524d\u9875\u9762\u6253\u5f00\u7684\u94fe\u63a5\uff0c\u624d\u53ef\u4ee5\u8bbfsessionStorage\u7684\u6570\u636e\uff0c\u4f7f\u7528window.open\u6253\u5f00\u9875\u9762\u548c\u6539\u53d8localtion.href\u65b9\u5f0f\u90fd\u53ef\u4ee5\u83b7\u53d6\u5230sessionStorage\u5185\u90e8\u7684\u6570\u636e;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u8005\u7684\u5f02\u540c<\/h2>\n\n\n\n<div class=\"wptb-table-container wptb-table-774\"><div class=\"wptb-table-container-matrix\"><table class=\"wptb-preview-table wptb-element-main-table_setting-774 edit-active\" data-reconstraction=\"0\" style=\"border: 1px solid rgb(209, 209, 209);\" data-wptb-table-tds-sum-max-width=\"525\" data-wptb-cells-width-auto-count=\"4\" role=\"table\" data-wptb-header-background-color=\"\" data-wptb-even-row-background-color=\"\" data-wptb-odd-row-background-color=\"\" data-wptb-fixed-width-size=\"0\" data-table-columns=\"4\"><tbody><tr class=\"wptb-row\" data-wptb-bg-color=\"\"><td class=\"wptb-cell\" style=\"border: 1px solid rgb(209, 209, 209);\" data-x-index=\"0\" data-y-index=\"0\" data-wptb-css-td-auto-width=\"true\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-6\"><div class=\"\" style=\"position: relative;\"><p>\u7279\u6027<\/p><\/div><\/div><\/td><td class=\"wptb-cell\" style=\"border: 1px solid rgb(209, 209, 209);\" data-x-index=\"1\" data-y-index=\"0\" data-wptb-css-td-auto-width=\"true\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-7\"><div class=\"\" style=\"position: relative;\"><p>Cookie<\/p><\/div><\/div><\/td><td class=\"wptb-cell\" style=\"border: 1px solid rgb(209, 209, 209);\" data-x-index=\"2\" data-y-index=\"0\" data-wptb-css-td-auto-width=\"true\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-8\"><div class=\"\" style=\"position: relative;\"><p>localStorage<\/p><\/div><\/div><\/td><td class=\"wptb-cell\" style=\"border: 1px solid rgb(209, 209, 209);\" data-x-index=\"3\" data-y-index=\"0\" data-wptb-css-td-auto-width=\"true\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-9\"><div class=\"\" style=\"position: relative;\"><p>sessionStorage<\/p><\/div><\/div><\/td><\/tr><tr class=\"wptb-row\" data-wptb-bg-color=\"\"><td class=\"wptb-cell\" data-y-index=\"1\" data-x-index=\"0\" style=\"border: 1px solid rgb(209, 209, 209);\" data-wptb-css-td-auto-width=\"true\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-1\"><div class=\"\" style=\"position: relative;\"><p>\u4f5c\u7528\u4e0e\u7279\u6027<\/p><\/div><\/div><\/td><td class=\"wptb-cell\" data-y-index=\"1\" data-x-index=\"1\" style=\"border: 1px solid rgb(209, 209, 209);\" data-wptb-css-td-auto-width=\"true\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-1\"><div class=\"\" style=\"position: relative;\"><p>\u25cf\u5b58\u50a8\u7528\u6237\u4fe1\u606f\uff0c\u83b7\u53d6\u6570\u636e\u9700\u8981\u4e0e\u670d\u52a1\u5668\u5efa\u7acb\u8fde\u63a5<\/p><\/div><\/div><div class=\"wptb-text-container wptb-ph-element wptb-element-text-17\"><div class=\"\" style=\"position: relative;\"><p>\u25cf\u53ef\u5b58\u50a8\u7684\u6570\u636e\u6709\u9650\uff0c\u4e14\u4f9d\u8d56\u4e8e\u670d\u52a1\u5668\uff0c\u65e0\u9700\u8bf7\u6c42\u670d\u52a1\u5668\u7684\u6570\u636e\u5c3d\u91cf\u4e0d\u8981\u5b58\u653e\u5728cookie\u4e2d\uff0c\u4ee5\u514d\u5f71\u54cd\u9875\u9762\u6027\u80fd\u3002<\/p><\/div><\/div><div class=\"wptb-text-container wptb-ph-element wptb-element-text-18\"><div class=\"\" style=\"position: relative;\"><p>\u25cf\u53ef\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f4<\/p><\/div><\/div><\/td><td class=\"wptb-cell\" data-y-index=\"1\" data-x-index=\"2\" style=\"border: 1px solid rgb(209, 209, 209);\" data-wptb-css-td-auto-width=\"true\" colspan=\"1\" rowspan=\"1\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-1\"><div class=\"\" style=\"position: relative;\"><p><\/p><\/div><\/div><div class=\"wptb-text-container wptb-ph-element wptb-element-text-2\"><div class=\"\" style=\"position: relative;\"><p>\u25cf\u5b58\u50a8\u5ba2\u6237\u7aef\u4fe1\u606f\uff0c\u65e0\u9700\u8bf7\u6c42\u670d\u52a1\u5668<\/p><\/div><\/div><div class=\"wptb-text-container wptb-ph-element wptb-element-text-19\"><div class=\"\" style=\"position: relative;\"><p>\u25cf\u6570\u636e\u6c38\u4e45\u4fdd\u5b58\uff0c\u9664\u975e\u7528\u6237\u624b\u52a8\u6e05\u7406\u5ba2\u6237\u7aef\u7f13\u5b58<\/p><\/div><\/div><div class=\"wptb-text-container wptb-ph-element wptb-element-text-20\"><div class=\"\" style=\"position: relative;\"><p>\u25cf\u5f00\u53d1\u8005\u53ef\u81ea\u884c\u5c01\u88c5\u4e00\u4e2a\u65b9\u6cd5\uff0c\u8bbe\u7f6e\u5931\u6548\u65f6\u95f4<\/p><\/div><\/div><\/td><td class=\"wptb-cell\" style=\"border: 1px solid rgb(209, 209, 209);\" data-x-index=\"3\" data-y-index=\"1\" data-wptb-css-td-auto-width=\"true\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-14\"><div class=\"\" style=\"position: relative;\"><p>\u25cf \u5b58\u50a8\u5ba2\u6237\u7aef\u4fe1\u606f\uff0c\u65e0\u9700\u8bf7\u6c42\u670d\u52a1\u5668\u3002<\/p><\/div><\/div><div class=\"wptb-text-container wptb-ph-element wptb-element-text-21\"><div class=\"\" style=\"position: relative;\"><p>\u25cf \u6570\u636e\u4fdd\u5b58\u5728\u5f53\u524d\u4f1a\u8bdd\uff0c\u5237\u65b0\u9875\u9762\u6570\u636e\u4e0d\u4f1a\u88ab\u6e05\u9664\uff0c\u7ed3\u675f\u4f1a\u8bdd\uff08\u5173\u95ed\u6d4f\u89c8\u5668\u3001\u5173\u95ed\u9875\u9762\u3001\u8df3\u8f6c\u9875\u9762\uff09\u6570\u636e\u5931\u6548\u3002<\/p><\/div><\/div><\/td><\/tr><tr class=\"wptb-row\" data-wptb-bg-color=\"\"><td class=\"wptb-cell\" data-y-index=\"2\" data-x-index=\"0\" style=\"border: 1px solid rgb(209, 209, 209);\" data-wptb-css-td-auto-width=\"true\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-1\"><div class=\"\" style=\"position: relative;\"><p>\u5b58\u653e\u6570\u636e\u5927\u5c0f<\/p><\/div><\/div><\/td><td class=\"wptb-cell\" data-y-index=\"2\" data-x-index=\"1\" style=\"border: 1px solid rgb(209, 209, 209);\" data-wptb-css-td-auto-width=\"true\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-15\"><div class=\"\" style=\"position: relative;\"><p>\u25cf\u6700\u597d\u5c06cookie\u63a7\u5236\u57284095B\u4ee5\u5185\uff0c\u8d85\u51fa\u7684\u6570\u636e\u4f1a\u88ab\u5ffd\u7565<br><\/p><\/div><\/div><div class=\"wptb-text-container wptb-ph-element wptb-element-text-16\"><div class=\"\" style=\"position: relative;\"><p>\u25cfIE6\u6216\u66f4\u4f4e\u7248\u672c\u6700\u591a\u5b5820\u4e2acookie\uff1b IE7\u53ca\u4ee5\u4e0a\u7248\u672c\u6700\u591a\u53ef\u4ee5\u670950\u4e2a\uff1bFirefox\u6700\u591a50\u4e2a\uff1bchrome\u548cSafari\u6ca1\u6709\u505a\u786c\u6027\u9650\u5236<\/p><\/div><\/div><\/td><td class=\"wptb-cell\" data-y-index=\"2\" data-x-index=\"2\" style=\"border: 1px solid rgb(209, 209, 209);\" data-wptb-css-td-auto-width=\"true\" colspan=\"2\" rowspan=\"1\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-1\"><div class=\"\" style=\"position: relative;\"><p>5M\u5de6\u53f3\uff0c\u5404\u6d4f\u89c8\u5668\u7684\u5b58\u50a8\u7a7a\u95f4\u6709\u5dee\u5f02<\/p><\/div><\/div><\/td><\/tr><tr class=\"wptb-row\" data-wptb-bg-color=\"\"><td class=\"wptb-cell\" data-y-index=\"3\" data-x-index=\"0\" style=\"border: 1px solid rgb(209, 209, 209);\" data-wptb-css-td-auto-width=\"true\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-1\"><div class=\"\" style=\"position: relative;\"><p>\u4e0e\u670d\u52a1\u5668\u7aef\u901a\u4fe1<\/p><\/div><\/div><\/td><td class=\"wptb-cell\" data-y-index=\"3\" data-x-index=\"1\" style=\"border: 1px solid rgb(209, 209, 209);\" data-wptb-css-td-auto-width=\"true\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-1\"><div class=\"\" style=\"position: relative;\"><p>\u6bcf\u6b21\u90fd\u4f1a\u643a\u5e26\u5728HTTP\u5934\u4e2d\uff0c\u5982\u679c\u4f7f\u7528cookie\u4fdd\u5b58\u8fc7\u591a\u6570\u636e\u4f1a\u5e26\u6765\u6027\u80fd\u95ee\u9898<\/p><\/div><\/div><\/td><td class=\"wptb-cell\" data-y-index=\"3\" data-x-index=\"2\" style=\"border: 1px solid rgb(209, 209, 209);\" data-wptb-css-td-auto-width=\"true\" colspan=\"2\" rowspan=\"1\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-1\"><div class=\"\" style=\"position: relative;\"><p>\u4ec5\u5728\u5ba2\u6237\u7aef\uff08\u5373\u6d4f\u89c8\u5668\uff09\u4e2d\u4fdd\u5b58\uff0c\u4e0d\u53c2\u4e0e\u548c\u670d\u52a1\u5668\u7684\u901a\u4fe1<\/p><\/div><\/div><\/td><\/tr><tr class=\"wptb-row\" data-wptb-bg-color=\"\"><td class=\"wptb-cell\" style=\"border: 1px solid rgb(209, 209, 209);\" data-x-index=\"0\" data-y-index=\"4\" data-wptb-css-td-auto-width=\"true\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-10\"><div class=\"\" style=\"position: relative;\"><p>\u6613\u7528\u6027<\/p><\/div><\/div><\/td><td class=\"wptb-cell\" style=\"border: 1px solid rgb(209, 209, 209);\" data-x-index=\"1\" data-y-index=\"4\" data-wptb-css-td-auto-width=\"true\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-11\"><div class=\"\" style=\"position: relative;\"><p>\u9700\u8981\u7a0b\u5e8f\u5458\u81ea\u5df1\u5c01\u88c5\uff0c\u6e90\u751f\u7684Cookie\u63a5\u53e3\u4e0d\u53cb\u597d($.cookie)<\/p><\/div><\/div><\/td><td class=\"wptb-cell\" style=\"border: 1px solid rgb(209, 209, 209);\" data-x-index=\"2\" data-y-index=\"4\" data-wptb-css-td-auto-width=\"true\" colspan=\"2\" rowspan=\"1\"><div class=\"wptb-text-container wptb-ph-element wptb-element-text-12\"><div class=\"\" style=\"position: relative;\"><p>\u6e90\u751f\u63a5\u53e3\u53ef\u4ee5\u63a5\u53d7\uff0c\u4ea6\u53ef\u518d\u6b21\u5c01\u88c5\u6765\u5bf9Object\u548cArray\u6709\u66f4\u597d\u7684\u652f\u6301<\/p><\/div><\/div><\/td><\/tr><\/tbody><\/table><\/div><\/div>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"\u5e94\u7528\u573a\u666f\"><strong>\u5e94\u7528\u573a\u666f<\/strong><\/h4>\n\n\n\n<p>\u6709\u4e86\u5bf9\u4e0a\u9762\u8fd9\u4e9b\u5dee\u522b\u7684\u76f4\u89c2\u7406\u89e3\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u8ba8\u8bba\u4e09\u8005\u7684\u5e94\u7528\u573a\u666f\u4e86\u3002<\/p>\n\n\n\n<p>\u56e0\u4e3a\u8003\u8651\u5230\u6bcf\u4e2a HTTP \u8bf7\u6c42\u90fd\u4f1a\u5e26\u7740 Cookie \u7684\u4fe1\u606f\uff0c\u6240\u4ee5 Cookie \u5f53\u7136\u662f\u80fd\u7cbe\u7b80\u5c31\u7cbe\u7b80\u5566\uff0c\u6bd4\u8f83\u5e38\u7528\u7684\u4e00\u4e2a\u5e94\u7528\u573a\u666f\u5c31\u662f\u5224\u65ad\u7528\u6237\u662f\u5426\u767b\u5f55\u3002\u9488\u5bf9\u767b\u5f55\u8fc7\u7684\u7528\u6237\uff0c\u670d\u52a1\u5668\u7aef\u4f1a\u5728\u4ed6\u767b\u5f55\u65f6\u5f80 Cookie \u4e2d\u63d2\u5165\u4e00\u6bb5\u52a0\u5bc6\u8fc7\u7684\u552f\u4e00\u8fa8\u8bc6\u5355\u4e00\u7528\u6237\u7684\u8fa8\u8bc6\u7801\uff0c\u4e0b\u6b21\u53ea\u8981\u8bfb\u53d6\u8fd9\u4e2a\u503c\u5c31\u53ef\u4ee5\u5224\u65ad\u5f53\u524d\u7528\u6237\u662f\u5426\u767b\u5f55\u5566\u3002\u66fe\u7ecf\u8fd8\u4f7f\u7528 Cookie \u6765\u4fdd\u5b58\u7528\u6237\u5728\u7535\u5546\u7f51\u7ad9\u7684\u8d2d\u7269\u8f66\u4fe1\u606f\uff0c\u5982\u4eca\u6709\u4e86 localStorage\uff0c\u4f3c\u4e4e\u5728\u8fd9\u4e2a\u65b9\u9762\u4e5f\u53ef\u4ee5\u7ed9 Cookie \u653e\u4e2a\u5047\u4e86~<\/p>\n\n\n\n<p>\u800c\u53e6\u4e00\u65b9\u9762 localStorage \u63a5\u66ff\u4e86 Cookie \u7ba1\u7406\u8d2d\u7269\u8f66\u7684\u5de5\u4f5c\uff0c\u540c\u65f6\u4e5f\u80fd\u80dc\u4efb\u5176\u4ed6\u4e00\u4e9b\u5de5\u4f5c\u3002\u6bd4\u5982HTML5\u6e38\u620f\u901a\u5e38\u4f1a\u4ea7\u751f\u4e00\u4e9b\u672c\u5730\u6570\u636e\uff0clocalStorage \u4e5f\u662f\u975e\u5e38\u9002\u7528\u7684\u3002\u5982\u679c\u9047\u5230\u4e00\u4e9b\u5185\u5bb9\u7279\u522b\u591a\u7684\u8868\u5355\uff0c\u4e3a\u4e86\u4f18\u5316\u7528\u6237\u4f53\u9a8c\uff0c\u6211\u4eec\u53ef\u80fd\u8981\u628a\u8868\u5355\u9875\u9762\u62c6\u5206\u6210\u591a\u4e2a\u5b50\u9875\u9762\uff0c\u7136\u540e\u6309\u6b65\u9aa4\u5f15\u5bfc\u7528\u6237\u586b\u5199\u3002\u8fd9\u65f6\u5019 sessionStorage \u7684\u4f5c\u7528\u5c31\u53d1\u6325\u51fa\u6765\u4e86\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5b89\u5168\u6027\u7684\u8003\u8651<\/h2>\n\n\n\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u4e0d\u662f\u4ec0\u4e48\u6570\u636e\u90fd\u9002\u5408\u653e\u5728 Cookie\u3001localStorage \u548c sessionStorage \u4e2d\u7684\u3002\u4f7f\u7528\u5b83\u4eec\u7684\u65f6\u5019\uff0c\u9700\u8981\u65f6\u523b\u6ce8\u610f\u662f\u5426\u6709\u4ee3\u7801\u5b58\u5728 XSS \u6ce8\u5165\u7684\u98ce\u9669\u3002\u56e0\u4e3a\u53ea\u8981\u6253\u5f00\u63a7\u5236\u53f0\uff0c\u4f60\u5c31\u968f\u610f\u4fee\u6539\u5b83\u4eec\u7684\u503c\uff0c\u4e5f\u5c31\u662f\u8bf4\u5982\u679c\u4f60\u7684\u7f51\u7ad9\u4e2d\u6709 XSS \u7684\u98ce\u9669\uff0c\u5b83\u4eec\u5c31\u80fd\u5bf9\u4f60\u7684 localStorage \u8086\u610f\u5984\u4e3a\u3002\u6240\u4ee5\u5343\u4e07\u4e0d\u8981\u7528\u5b83\u4eec\u5b58\u50a8\u4f60\u7cfb\u7edf\u4e2d\u7684\u654f\u611f\u6570\u636e\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">localStorage\u548csessionStorage\u64cd\u4f5c<\/h2>\n\n\n\n<p>localStorage\u548csessionStorage\u90fd\u5177\u6709\u76f8\u540c\u7684\u64cd\u4f5c\u65b9\u6cd5\uff0c\u4f8b\u5982setItem\u3001getItem\u548cremoveItem\u7b49<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>localStorage\u548csessionStorage\u7684\u65b9\u6cd5<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>setItem\u5b58\u50a8value<\/strong><\/h4>\n\n\n\n<p>\u7528\u9014\uff1a\u5c06value\u5b58\u50a8\u5230key\u5b57\u6bb5<\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nsessionStorage.setItem(&quot;key&quot;, &quot;value&quot;);     \nlocalStorage.setItem(&quot;site&quot;, &quot;js8.in&quot;);\n<\/pre>\n\n\n<h4 class=\"wp-block-heading\"><strong>getItem\u83b7\u53d6value<\/strong><\/h4>\n\n\n\n<p>\u7528\u9014\uff1a\u83b7\u53d6\u6307\u5b9akey\u672c\u5730\u5b58\u50a8\u7684\u503c<\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nvar value = sessionStorage.getItem(&quot;key&quot;);     \nvar site = localStorage.getItem(&quot;site&quot;);\n<\/pre>\n\n\n<h4 class=\"wp-block-heading\"><strong>removeItem\u5220\u9664key<\/strong><\/h4>\n\n\n\n<p>\u7528\u9014\uff1a\u5220\u9664\u6307\u5b9akey\u672c\u5730\u5b58\u50a8\u7684\u503c<\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nsessionStorage.removeItem(&quot;key&quot;);     \nlocalStorage.removeItem(&quot;site&quot;);\n<\/pre>\n\n\n<h4 class=\"wp-block-heading\"><strong>clear\u6e05\u9664\u6240\u6709\u7684key\/value<\/strong><\/h4>\n\n\n\n<p>\u7528\u9014\uff1a\u6e05\u9664\u6240\u6709\u7684key\/value<\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nsessionStorage.clear();     \nlocalStorage.clear();\n<\/pre>\n\n\n<h4 class=\"wp-block-heading\"><strong>\u5176\u4ed6\u64cd\u4f5c\u65b9\u6cd5\uff1a\u70b9\u64cd\u4f5c\u548c[ ]<\/strong><\/h4>\n\n\n\n<p>web Storage\u4e0d\u4f46\u53ef\u4ee5\u7528\u81ea\u8eab\u7684setItem,getItem\u7b49\u65b9\u4fbf\u5b58\u53d6\uff0c\u4e5f\u53ef\u4ee5\u50cf\u666e\u901a\u5bf9\u8c61\u4e00\u6837\u7528\u70b9(.)\u64cd\u4f5c\u7b26\uff0c\u53ca[]\u7684\u65b9\u5f0f\u8fdb\u884c\u6570\u636e\u5b58\u50a8\uff0c\u50cf\u5982\u4e0b\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nvar storage = window.localStorage; \nstorage.key1 = &quot;hello&quot;; \nstorage[&quot;key2&quot;] = &quot;world&quot;; \nconsole.log(storage.key1); \nconsole.log(storage[&quot;key2&quot;]);\n<\/pre>\n\n\n<h4 class=\"wp-block-heading\"><strong>localStorage\u548csessionStorage\u7684key\u548clength\u5c5e\u6027\u5b9e\u73b0\u904d\u5386<\/strong><\/h4>\n\n\n\n<p>sessionStorage\u548clocalStorage\u63d0\u4f9b\u7684key()\u548clength\u53ef\u4ee5\u65b9\u4fbf\u7684\u5b9e\u73b0\u5b58\u50a8\u7684\u6570\u636e\u904d\u5386\uff0c\u4f8b\u5982\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nvar storage = window.localStorage;\nfor(var i=0, len=storage.length; i&lt;len;i++){\n    var key = storage.key(i);     \n    var value = storage.getItem(key);     \n    console.log(key + &quot;=&quot; + value); \n}\n<\/pre>\n\n\n<h2 class=\"wp-block-heading\"> <strong>cookie<\/strong> \u64cd\u4f5c<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>\u81ea\u5b9a\u4e49\u5c01\u88c5<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"> <strong>\u521b\u5efacookie<\/strong><\/h4>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/**\n* @description js\u539f\u751f\u8bbe\u7f6ecookie\n* @param {String} name \u7ed9\u4f60\u8981\u8bbe\u7f6e\u7684cookie\u8d77\u4e2a\u540d\u5b57\uff08key\uff09\n* @param {String} value cookie\u7684\u5177\u4f53\u5185\u5bb9(value)\n* @param {String} expiredays \u8bbe\u7f6ecookie\u7684\u8fc7\u671f\u65f6\u95f4\uff0c\u5355\u4f4d\uff1a\u5929\n*\/\nfunction setCookie(name, value, expiredays) {\nvar exdate=new Date();\nexdate.setDate(exdate.getDate() + expiredays);\ndocument.cookie = name + '=' + escape(value)+ ((expiredays == null) ? '' : ';expires=' +exdate.toGMTString());\n}\n<\/pre>\n\n\n<h4 class=\"wp-block-heading\"> <strong>\u83b7\u53d6cookie<\/strong> <\/h4>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/**\n* @description js\u539f\u751f\u83b7\u53d6cookie\u65b9\u6cd51\n* @param {String} name \u4f60\u8981\u83b7\u53d6\u7684cookie\u540d\n*\/\nfunction getCookie(name) {\n\u3000\u3000if (document.cookie.length &gt; 0) {\n\u3000\u3000\u3000\u3000var start = document.cookie.indexOf(name + '=');\n\u3000\u3000\u3000\u3000if (start !== -1) {\n\u3000\u3000\u3000\u3000\u3000\u3000start = start + name.length + 1;\n\u3000\u3000\u3000\u3000\u3000\u3000var end = document.cookie.indexOf(';', start);\n\u3000\u3000\u3000\u3000\u3000\u3000if (end === -1) {\n\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000end = document.cookie.length;\n\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000return unescape(document.cookie.substring(start, end));\n\u3000\u3000\u3000\u3000\u3000\u3000}\n\u3000\u3000\u3000}\n\u3000\u3000}\n\u3000\u3000return '';}\n<\/pre>\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/**\n* @description js\u539f\u751f\u83b7\u53d6cookie\u65b9\u6cd52\n* @param {String} name \u4f60\u8981\u83b7\u53d6\u7684cookie\u540d\n*\/\nfunction getCookie(name) {\n\u3000\u3000var cookieArr = document.cookie.split(';') || [];\n\u3000\u3000if(!cookieArr.length){\n\u3000\u3000\u3000\u3000return '';\n\u3000\u3000}\n\u3000\u3000for(var i = 0; i &lt; cookieArr.length; i ++){\n\u3000\u3000\u3000\u3000var key = $.trim(cookieArr[i]).split('=')[0];\n\u3000\u3000\u3000\u3000var value = $.trim(cookieArr[i]).split('=')[1];\n\u3000\u3000\u3000\u3000if(key === name){\n\u3000\u3000\u3000\u3000\u3000\u3000return value;\n\u3000\u3000\u3000\u3000}\n\u3000\u3000}\n}\n<\/pre>\n\n\n<h4 class=\"wp-block-heading\"> <strong>\u68c0\u67e5cookie\u662f\u5426\u5df2\u5b58\u5728<\/strong> <\/h4>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nfunction checkCookie() {\n\u3000\u3000username = getCookie('username');\n\u3000\u3000if (username !== null &amp;amp;&amp;amp; username !== '') {\n\u3000\u3000\u3000\u3000alert('Welcome again ' + username + '!');\n\u3000\u3000} else {\n\u3000\u3000\u3000\u3000username = prompt('Please enter your name:', '');\n\u3000\u3000\u3000\u3000if (username !== null &amp;amp;&amp;amp; username !== '') {\n\u3000\u3000\u3000\u3000\u3000\u3000setCookie('username',username,365);\n\u3000\u3000\u3000\u3000}\n\u3000\u3000}\n}\n<\/pre>\n\n\n<h3 class=\"wp-block-heading\"> <strong>jquery.cookie.js\u5c01\u88c5<\/strong> <\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"> <strong>\u521b\u5efacookie<\/strong> <\/h4>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/**\n* 'name', cookie\u547d\u540d\n* 'value'\uff0ccookie\u7684\u503c\n* {\n* \u3000\u3000expires: 7, \/\/ cookie\u6709\u6548\u671f\uff0c\u5355\u4f4d\u5929\uff1b\u9ed8\u8ba4\u503c\uff1a\u4f1a\u8bddcookie\uff0c\u5173\u95ed\u6d4f\u89c8\u5668cookie\u5931\u6548\u3002\n* \u3000\u3000path: '\/', \/\/ cookie\u5f71\u54cd\u5230\u7684\u8def\u5f84\uff1b\u503c\u4e3a'\/'\uff0c\u8868\u793a\u8bbe\u7f6ecookie\u5728\u6574\u4e2a\u57df\u4e2d\u53ef\u7528\uff1b\u9ed8\u8ba4\u503c\uff1a\u521b\u5efacookie\u7684\u9875\u9762\u8def\u5f84\u3002\n* \u3000\u3000domain: 'example.com', \/\/ \u5b9a\u4e49cookie\u6709\u6548\u7684\u57df\u3002\u9ed8\u8ba4\u503c\uff1a\u521b\u5efacookie\u7684\u9875\u9762\u57df\u3002\n* \u3000\u3000secure: false, \/\/ \u5b9a\u4e49cookie\u5b89\u5168\u6027\uff0c\u9ed8\u8ba4\u503c\uff1afalse\uff0c\u8bbe\u7f6e\u4e3atrue\uff0c\u5219cookie\u5728http\u4e2d\u662f\u65e0\u6548\u7684\uff0ccookie\u7684\u4f20\u8f93\u9700\u8981\u4f7f\u7528\u5b89\u5168\u534f\u8bae\uff08https\uff09\u3002\n* }\n*\/\n$.cookie('name', 'value', { expires: 7, path: '\/', domain: 'example.com', secure: false});\n<\/pre>\n\n\n<h4 class=\"wp-block-heading\"> <strong>\u8bfb\u53d6cookie<\/strong> <\/h4>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n$.cookie('name'); \/\/name\u5b58\u5728\u8fd4\u56de\u5bf9\u5e94value,\u4e0d\u5b58\u5728\u8fd4\u56denull\n<\/pre>\n\n\n<h4 class=\"wp-block-heading\"> <strong>\u5220\u9664cookie<\/strong> <\/h4>\n\n\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\/\/\u6210\u529f\u5220\u9664cookie\u65f6\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\n$.removeCookie('name'); \/\/ =&gt; true\n$.removeCookie('nothing'); \/\/ =&gt; false\n\u6ce8\u610f\uff1a\u5220\u9664cookie\u65f6\uff0c\u5fc5\u987b\u4f20\u9012\u7528\u4e8e\u8bbe\u7f6ecookie\u7684\u5b8c\u5168\u76f8\u540c\u7684\u8def\u5f84\uff0c\u57df\u548c\u5b89\u5168\u9009\u9879\uff0c\u9664\u975e\u60a8\u4f9d\u8d56\u4e8e\u9ed8\u8ba4\u9009\u9879\u3002\n\u5373\uff1a\u8bbe\u7f6ecookie\u65f6\u5982\u679c\u8bbe\u7f6e\u4e86path\u5c5e\u6027\u6216secure\u5c5e\u6027\uff0c\u5220\u9664\u7684\u65f6\u5019\u8981\u5e26\u7740\u8fd9\u4e9b\u5c5e\u6027\uff0c\u5426\u5219\u65e0\u6cd5\u6210\u529f\u5220\u9664cookie\u3002\n\/\/ This won't work!\n$.removeCookie('name'); \/\/ =&gt; false\n\/\/ This will work!\n$.removeCookie('name', { path: '\/' }); \/\/ =&gt; true\nTODO\uff1a$.removeCookie\u65e0\u6548\uff1f\uff08jquery1.9.1.js + jquery.cookie.js\uff0c\u63d2\u4ef6\u65e0\u91cd\u590d\u5f15\u7528\u7684\u60c5\u51b5\uff0c$.removeCookie\u63d0\u793aundefined\uff09\n<\/pre>\n\n\n<h2 class=\"wp-block-heading\">\u53c2\u8003\u6587\u6863\uff1a <\/h2>\n\n\n\n<p><strong>Cookie API<\/strong><br><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Cookies\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Cookies<\/a><br><a href=\"http:\/\/www.w3school.com.cn\/js\/js_cookies.asp\">http:\/\/www.w3school.com.cn\/js\/js_cookies.asp<\/a><br><a href=\"https:\/\/github.com\/carhartl\/jquery-cookie#readme\">https:\/\/github.com\/carhartl\/jquery-cookie#readme<\/a><\/p>\n\n\n\n<p><strong>webStorage<\/strong><br><a href=\"http:\/\/www.w3school.com.cn\/html5\/html_5_webstorage.asp\">http:\/\/www.w3school.com.cn\/html5\/html_5_webstorage.asp<\/a><br><a href=\"https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/API\/Web_Storage_API\">https:\/\/developer.mozilla.org\/zh-CN\/docs\/Web\/API\/Web_Storage_API<\/a><\/p>\n\n\n\n<p><\/p>\n<!--wp-compress-html no compression--><!--wp-compress-html-->","protected":false},"excerpt":{"rendered":"\u57fa\u672c\u6982\u5ff5 Cookie Cookie\u662f\u670d\u52a1\u5668\u53d1\u9001\u5230\u6d4f\u89c8\u5668\u7684\u4e00\u5c0f\u6bb5\u6570\u636e\uff0c\u4f1a\u5728\u6d4f\u89c8\u5668\u4e0b\u6b21\u5411\u540c\u4e00\u670d\u52a1\u5668\u518d\u53d1\u8d77\u8bf7\u6c42\u65f6\u88ab\u643a\u5e26\u5e76\u53d1\u9001\u5230\u670d\u52a1\u5668\u4e0a\u3002 Cookie\u8bde\u751f\u4e4b\u521d\u7684\u4f5c\u7528\u5c31\u662f\u89e3\u51b3HTTP\u7684\u65e0\u72b6\u6001\u8bf7\u6c42\uff0c\u7528\u6765\u8bb0\u5f55\u4e00 \u00b7\u00b7\u00b7","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52],"tags":[94,95,96],"table_tags":[],"_links":{"self":[{"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/posts\/772"}],"collection":[{"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/comments?post=772"}],"version-history":[{"count":7,"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/posts\/772\/revisions"}],"predecessor-version":[{"id":783,"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/posts\/772\/revisions\/783"}],"wp:attachment":[{"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/media?parent=772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/categories?post=772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/tags?post=772"},{"taxonomy":"table_tags","embeddable":true,"href":"https:\/\/www.shineskr.com\/api\/wp\/v2\/table_tags?post=772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}