当前位置:网站首页>浏览器本地存储
浏览器本地存储
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。
边栏推荐
- 【编程强训2】排序子序列+倒置字符串
- Kdtree notes
- Buildreoot override mechanism
- Why did grayscale fall from the altar?
- ctfshow-web352,353(SSRF)
- 【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
- [the path of system analysts] Chapter 5: software engineering of double disk (reverse clean room and Model Driven Development)
- 开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
- 运维面临挑战?智能运维管理系统来帮您
- Warm congratulations on the successful listing of five elements hehe liquor
猜你喜欢

Microsoft announces open source (Godel) language model chat robot

图像风格迁移 CycleGAN原理

【FPGA帧差】基于VmodCAM摄像头的帧差法目标跟踪FPGA实现

【编程强训2】排序子序列+倒置字符串

【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算

灰度何以跌下神坛?

如何制作专属的VS Code主题

The computer has a network, but all browser pages can't be opened. What's the matter?

【推荐系统】美团外卖推荐场景的深度位置交互网络DPIN的突破与畅想

盘点华为云GaussDB(for Redis)六大秒级能力
随机推荐
ctfshow-web351(SSRF)
比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛
Reply and explanation on issues related to "online training of network security education in 2022"
代码实战——从零开始搭建自己的Diffusion models/Score-based generative models
8 figures | analyze Eureka's first synchronization registry
Alibaba OSS postman invalid according to policy: policy condition failed: ["starts with", "key", "test/"]
[lingo] find the minimum connection diagram of seven cities to minimize the price of natural gas pipelines
北漂程序员深夜emo发帖求助:女朋友走了我很孤独 ......
[the path of system analysts] Chapter 5: software engineering of double disk (reverse clean room and Model Driven Development)
转行做产品经理,如何挑选产品经理课程?
C语言实现【扫雷游戏】完整版(实现源码)
[matlab] solve nonlinear programming
运维面临挑战?智能运维管理系统来帮您
1286_FreeRTOS的任务优先级设置实现分析
Solution to the problem that objects in unity2021 scene view cannot be directly selected
[Tikhonov] image super-resolution reconstruction based on Tikhonov regularization
C# 读写自定义的Config文件
【目标检测】目标检测界的扛把子YOLOv5(原理详解+修炼指南)
[lingo] find the shortest path problem of undirected graph
iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
