cookie的缺点:
- Cookie 的大小被限制为 4KB;
- Cookie 会包含在每个HTTP请求中向服务器发送,这样势必导致多次发送重复数据;
- Cookie 在网络传输时并未加密(除非整个应用都使用SSL),因此可能存在一些安全隐患。
HTML5的Web Storage:
HTML5 新增了Web Storage功能。通过Web Storage,可以让应用程序在客户端运行时在客户端保存程序数据。相关方法如下:
- length :该属性返回Storage里保存了多少组键值对;
- key(index):该方法返回该Storage中第index个key值;
- getItem(key):该方法返回该Storage中指定key对应的value;
- setItem(key,value):该方法用于向该Storage存入指定的 key-value 对;
- removeItem():该方法用于从该Storage删除指定key对应的 key-value 对;
- clear( );该方法用于删除该Storage中所有的 key-value 的对。
web storage 分为:localStorage和sessionStorage两种;localStorage长期保存本地,除非用户自己删除;sessionStorage存储时间为会话期间,关闭浏览器就删除了。