当前位置:网站首页>浏览器存储WebStorage
浏览器存储WebStorage
2022-08-05 05:22:00 【-小龙人】
介绍
浏览器存储WebStorage包括 LocalStorage 和 SessionStorage,存储内容大小一般在5M左右。
如何使用
浏览器通过Window.localStorage 和 Window.sessionStorage属性来实现本地存储机制,在实际使用中可省略Window。
API使用
LocalStorage 和 SessionStorage的API方法名称用法相同,分别有新增、获取、删除、清空四个方法,方法名称及使用方法见下面示例:
//新增WebStorage: setItem(key,val)
localStorage.setItem("a", "123");
localStorage.setItem("b", "456");
localStorage.setItem("c", "789");
sessionStorage.setItem("a", "123");
sessionStorage.setItem("b", "456");
sessionStorage.setItem("c", "789");
//获取WebStorage: getItem(key)
localStorage.getItem("a");
sessionStorage.getItem("a");
//删除WebStorage: removeItem(key)
localStorage.removeItem("c");
sessionStorage.removeItem("c");
//清空WebStorage: removeItem(key)
localStorage.clear();
sessionStorage.clear();
LocalStorage 和 SessionStorage区别
同为浏览器储存的WebStorage,LocalStorage 和 SessionStorage使用的API相同,但实际使用场景是有区别的:
LocalStorage
使用localStorage保存的数据会长期保留,即使你关闭浏览器下次重新打开该页面,保存的数据依然还在不会被清空,如果需要清空必须手动清除。而且在同个浏览器相同源(window.location.origin)的其他页面都是可以通过 localStorage 访问到,当然了源与源之间的 localStorage 是不能互相访问的。即每个源之间的 localStorage 是相互独立的。SessionStorage
使用sessionStorage保存的数据只是当前页面会保留,即页面会话级别,当关闭浏览器或者关闭页签后sessionStorage保存的数据会被清空,而且在同个浏览器即使相同源,其他页面不可以共享sessionStorage保存的数据。
边栏推荐
- Getting Started Doc 06 Adding files to a stream
- IP地址及子网的划分
- 智能运维会取代人工运维吗?
- I/O performance and reliability
- [Pytorch study notes] 8. How to use WeightedRandomSampler (weight sampler) when the training category is unbalanced data
- Growth: IT Operations Trends Report
- 网络层协议介绍
- 传输层协议(TCP3次握手)
- vim的三种模式
- What impact does CIPU have on the cloud computing industry?
猜你喜欢
随机推荐
Account and Permission Management
LinkSLA insists that users come first and creates a sustainable operation and maintenance service plan
vim教程:vimtutor
监控系统的内卷,有什么讲究?
Spark source code - task submission process - 6-sparkContext initialization
ALC实验
Tencent Cloud Message Queue CMQ
Proprietary host CDH
The spark operator - coalesce operator
Apache configure reverse proxy
入门文档04 一个任务依赖另外一个任务时,需要按顺序执行
King power volume LinkSLA, realize operations engineer is happy fishing
深度 Zabbix 使用指南——来自惨绿少年
入门文档07 分阶段输出
sql server 重复值在后面计数
VRRP principle and command
磁盘管理与文件系统
Advantages of overseas servers
[Day8] (Super detailed steps) Use LVM to expand capacity
Unity realizes first-person roaming (nanny-level tutorial)
![[Day8] Commands involved in using LVM to expand](/img/ba/39b81cbcecec9bc54a710ff9dba81a.png)








