Cookies sessionStorage localStorage
从以下6个方面分析客户端存储的差异
// 服务端
// Set-Cookie: =
// 浏览器
document.cookie = "yummy_cookie=choco";
// Cookies的库
var docCookies = {
getItem: function (sKey) {
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[-.+*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
},
setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
var sExpires = "";
if (vEnd) {
switch (vEnd.constructor) {
case Number:
sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
break;
case String:
sExpires = "; expires=" + vEnd;
break;
case Date:
sExpires = "; expires=" + vEnd.toUTCString();
break;
}
}
document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
return true;
},
removeItem: function (sKey, sPath, sDomain) {
if (!sKey || !this.hasItem(sKey)) { return false; }
document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + ( sDomain ? "; domain=" + sDomain : "") + ( sPath ? "; path=" + sPath : "");
return true;
},
hasItem: function (sKey) {
return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[-.+*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
},
keys: /* optional method: you can safely remove it! */ function () {
var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
for (var nIdx = 0; nIdx < aKeys.length; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
return aKeys;
}
};
作为 Web Storage API 的接口,Storage 提供了访问特定域名下的会话存储
或本地存储
的功能
如果你想要操作一个域名的会话存储,可以使用 Window.sessionStorage
;
如果想要操作一个域名的本地存储,可以使用 Window.localStorage
。
// 抽象类
abstract class Storage {
length // readonly 存储在 Storage 对象中的数据项数量
key() {
// 该方法接受一个数值 n 作为参数,并返回存储中的第 n 个键名
}
getItem() {
// 获取
}
setItem() {
// 设置
}
removeItem() {
// 移除
}
clear() {
// 删除所有
}
}
class sessionStorage extends Storage {
}
class localStorage extends Storage {
}
Cookie 4K
storage 每个源的存储大小将限制在10M
Set-Cookie: id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT;
Domain 和 Path 标识定义了Cookie的作用域:即允许 Cookie 应该发送给哪些URL
Domian: 指定了哪些主机可以接受 Cookie
Path 标识指定了主机下的哪些路径可以接受 Cookie
SameSite: SameSite Cookie 允许服务器要求某个 cookie 在跨站请求时不会被发送
Cookie prefixes:
Secure HttpOnly
通过 Document.cookie 属性可访问 此方法新建的 Cookie 和 非httpOnly的Cookie
通过 JavaScript 创建的 Cookie 不能包含 HttpOnly 标志
storage :同源策略的限制