当前位置:网站首页>浏览器本地存储
浏览器本地存储
2022-07-01 07:17:00 【十八岁讨厌编程】
场景引入
首先我们可以通过一个场景来理解什么是浏览器的本地存储
例如我们打开淘宝搜索psp(不登陆账号的情况下),然后关闭网站之后,我们重新打开淘宝,可以发现在搜索历史里面可以看到psp。
为什么会这个样子呢?这里就是网站借助了浏览器的本地存储,将数据存储到了计算机的硬盘上。
我们可以借助浏览器的开发者工具查看:

同样的我们也可以在浏览器中存储东西!
浏览器本地存储
Web 存储 API 提供了 sessionStorage (会话存储) 和 localStorage(本地存储)两个存储对象来对网页的数据进行添加、删除、修改、查询操作。
- localStorage 用于长久保存整个网站的数据,保存的数据没有过期时间,直到手动去除。(你把浏览器关闭掉也不会消失)
- sessionStorage 用于临时保存同一窗口(或标签页)的数据,在关闭窗口或标签页之后将会删除这些数据。
localStorage数据删除只有两种方式:
①程序员使用相关清除方法
②用户手动删除浏览器的缓存数据
相应的API及属性:
window.localStorage
语法:
window.localStorage
保存数据语法:
localStorage.setItem("key", "value");
键值对最终都会被存储为字符串
读取数据语法:
var lastname = localStorage.getItem("key");
删除数据语法:
localStorage.removeItem("key");
清空数据语法:
localStorage.clear();
我们现在创建一个网页并往上面长久的保存一些数据:
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<button onclick="storeData()">点击我在网页上存储数据</button>
</body>
<script> function storeData() {
localStorage.setItem('张三','18') } </script>
</html>

如果我们要存储一个对象:
<body>
<button onclick="storeData()">点击我在网页上存储数据</button>
</body>
<script> let person = {
name:'Tom',age:'20'} function storeData() {
localStorage.setItem('张三',6666) localStorage.setItem('msg',JSON.stringify(person)) } </script>

下面把几个api都使用一下:
<body>
<button onclick="storeData()">点击我在网页上存储数据</button>
<button onclick="watchData()">点击我在网页上数据</button>
<button onclick="deleteData()">点击我在网页上删除数据</button>
<button onclick="clearData()">点击我在网页上清空数据</button>
</body>
<script> let person = {
name:'Tom',age:'20'} function storeData() {
localStorage.setItem('张三',6666) localStorage.setItem('msg',JSON.stringify(person)) } function watchData() {
console.log(localStorage.getItem('张三')) console.log(JSON.parse(localStorage.getItem('msg'))) } function deleteData() {
localStorage.removeItem('张三') } function clearData() {
localStorage.clear() } </script>
对象如果直接查看会以字符串的展示出来,显然这不是我们想要的,我们可以使用JSON.parse进行分析,就可以重新得到我们想要的对象。
如果查看不存在的数据,会显示null。
JSON.parse(null)的结果还是null
window.sessionStorage
与window.localStorage的使用方法完全一致
不同在于:sessionStorage 用于临时保存同一窗口(或标签页)的数据,在关闭窗口或标签页之后将会删除这些数据。
总结
webStorage
存储内容大小一般支持5MB左右(不同浏览器可能还不一样)
浏览器端通过 Window.sessionStorage 和 Window.localStorage 属性来实现本地存储机制。
相关API:
xxxxxStorage.setItem('key', 'value');
该方法接受一个键和值作为参数,会把键值对添加到存储中,如果键名存在,则更新其对应的值。xxxxxStorage.getItem('person'); 该方法接受一个键名作为参数,返回键名对应的值。
xxxxxStorage.removeItem('key'); 该方法接受一个键名作为参数,并把该键名从存储中删除。
xxxxxStorage.clear() 该方法会清空存储中的所有数据。
备注:
- SessionStorage存储的内容会随着浏览器窗口关闭而消失。
- LocalStorage存储的内容,需要手动清除才会消失。
xxxxxStorage.getItem(xxx)如果xxx对应的value获取不到,那么getItem的返回值是null。JSON.parse(null)的结果依然是null。
边栏推荐
- 比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛
- How to permanently configure local opencv4.5.5 for vs2019
- Unity2021-Scene视图中物体无法直接选中的解决办法
- Microsoft announces open source (Godel) language model chat robot
- Is it safe to do fund fixed investment on Great Wall Securities?
- The programmer of Beipiao posted a post for help late at night: I am lonely when my girlfriend is gone
- Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing
- 华为ModelArts训练Alexnet模型
- DC-4靶机
- 熱烈祝賀五行和合酒成功掛牌
猜你喜欢

ctfshow-web354(SSRF)

【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真

【图像处理】图像直方图均衡化系统含GUI界面

JSP - paging

How to enter the Internet industry and become a product manager? How to become a product manager without project experience?
![[recommendation technology] matlab simulation of network information recommendation technology based on collaborative filtering](/img/fb/dc03f97f12488e53d706a05da9faea.png)
[recommendation technology] matlab simulation of network information recommendation technology based on collaborative filtering

DC-4 target

【计网】(一) 集线器、网桥、交换机、路由器等概念

ctfshow-web355,356(SSRF)

继妹变继母,儿子与自己断绝关系,世界首富马斯克,为何这么惨?
随机推荐
女生适合学产品经理吗?有什么优势?
[matlab] solve nonlinear programming
Are there any practical skills for operation and maintenance management
如何画产品架构图?
rclone中文文档:常用命令大全
Is it suitable for girls to study product manager? What are the advantages?
【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真
Why did grayscale fall from the altar?
用手机在指南针上开户靠谱吗?这样有没有什么安全隐患
ctfshow-web351(SSRF)
The programmer of Beipiao posted a post for help late at night: I am lonely when my girlfriend is gone
【深圳IO】精确食品称(汇编语言的一些理解)
How to enter the Internet industry and become a product manager? How to become a product manager without project experience?
Challenges faced by operation and maintenance? Intelligent operation and maintenance management system to help you
Subclasses call methods and properties of the parent class with the same name
【图像处理】图像直方图均衡化系统含GUI界面
关于“2022年度网络安全教育线上培训”相关问题的复盘和说明
在券商账户上买基金安全吗
Is it safe to do fund fixed investment on Great Wall Securities?
【LINGO】求无向图的最短路问题
