当前位置:网站首页>localStorage与sessionStorage
localStorage与sessionStorage
2022-07-27 00:28:00 【io无心】
cookie/localStorage/sessionStorage之间的区别
- 保存方式区别:cookie数据始终在同源的http请求中携带,即cookie会在浏览器和服务器之间来回传递,但可以通过路径限制其只属于某个路径下有效。而sessionStorage和localStorage不会把数据发给服务器,仅在本地保存。
- 存储大小限制不同:cookie数据不能超过4K,只适合保存很小的数据,如会话标识。sessionStorage和localStorage可存储的大小在5M左右。
- 数据有效期不同:sessionStorage仅在当前浏览器窗口关闭前有效;localStorage始终有效,窗口或浏览器关闭也一直保存;cookie只在设置的cookie过期时间之前一直有效,即使窗口或浏览器关闭。
- 作用域不同:sessionStorage不在不同的浏览器窗口中共享,即使是同一个页面;localStorage和cookie在所有同源窗口中共享。
localStorage的方法
- localStorage.setItem("key","value"); 添加一个键值对
- localStorage.getItem("key"); 通过key查询value
- localStorage.removeItem("key"); 删除key对应的键值对
- localStorage.clear(); 清除全部的键值对
sessionStorage的方法(与localStorage方法一样)
- sessionStorage.setItem("key","value"); 添加一个键值对
- sessionStorage.getItem("key"); 通过key查询value
- sessionStorage.removeItem("key"); 删除key对应的键值对
- sessionStorage.clear(); 清除全部的键值对
边栏推荐
- Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
- Quick sort
- Mysql 5.7 取分组第一条
- 用最原始的方法纯手工实现常见的 20 个数组方法
- C language program compilation (preprocessing)
- Debezium系列之:基于debezium offset拉取历史数据,确保数据没有丢失
- 论构造函数的原型是谁
- Scheduling of processes
- Function stack frame explanation
- Which securities firm is safer to open an account and buy REITs funds?
猜你喜欢

Plato Farm通过LaaS协议Elephant Swap,为社区用户带来全新体验

CS224W fall 课程 ---- 1.1 why Graphs ?

QT编译出来的exe以管理员权限启动

C语言程序的编译(预处理)下

阿里云技术专家杨泽强:弹性计算云上可观测能力的构建

Favicon网页收藏图标在线制作PHP网站源码/ICO图片在线生成/支持多种图片格式转换

基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)

CuteOne:一款OneDrive多网盘挂载程序/带会员/同步等功能

智能指针shared_ptr、unique_ptr、weak_ptr

八皇后编程实现
随机推荐
How small programs help the new model of smart home ecology
快速排序(Quick sort)
How to do the system security test? Let's talk about it in detail
Rust web (I) -- self built TCP server
Arduinouno drive RGB module full color effect example
Web3.0世界知识体系分享-什么是Web3.0
[Ryu] common problems and solutions in installing Ryu
Knowledge points of test questions related to software testing
Lua函数之非全局函数
论构造函数的原型是谁
用最原始的方法纯手工实现常见的 20 个数组方法
idea中常用的快捷键
Swiperjs custom width
贪心——376. 摆动序列
Why do people like to rank things
Okaleido Tiger 7.27日登录Binance NFT,首轮已获不俗成绩
ZJCTF_login
调用JShaman的Web API接口,实现JS代码加密。
Ansible series: do not collect host information gather_ facts: False
对象创建的流程分析