当前位置:网站首页>浏览器本地存储
浏览器本地存储
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。
边栏推荐
- ctfshow-web351(SSRF)
- 华为ModelArts训练Alexnet模型
- [the path of system analysts] Chapter 5: software engineering of double disk (reverse clean room and Model Driven Development)
- Cadence OrCAD Capture “网络名”相同,但是未连接或连接错误的解放方案之nodename的用法
- [lingo] find the shortest path problem of undirected graph
- 1286_FreeRTOS的任务优先级设置实现分析
- C# Newtonsoft.Json中JObject的使用
- How to permanently configure local opencv4.5.5 for vs2019
- 微软宣布开源 (GODEL) 语言模型聊天机器人
- Subclasses call methods and properties of the parent class with the same name
猜你喜欢

比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛

Understanding of Turing test and Chinese Room

Product learning (III) - demand list
![[lingo] find the minimum connection diagram of seven cities to minimize the price of natural gas pipelines](/img/34/d2efae5b283cdc130d55f52cdff76d.png)
[lingo] find the minimum connection diagram of seven cities to minimize the price of natural gas pipelines

图像风格迁移 CycleGAN原理
![[network planning] (I) hub, bridge, switch, router and other concepts](/img/7b/fcef37496517c854ac1dbfb35fa3f4.png)
[network planning] (I) hub, bridge, switch, router and other concepts

ctfshow-web354(SSRF)

C语言实现【扫雷游戏】完整版(实现源码)

Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)

C语言实现【三子棋游戏】(步骤分析和实现源码)
随机推荐
Paging in servlets and JSPS
【微服务|openfeign】Feign的日志记录
【深圳IO】精确食品称(汇编语言的一些理解)
在券商账户上买基金安全吗
rclone 访问web界面
The game is real! China software cup releases a new industrial innovation competition, and schools and enterprises can participate in it jointly
【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算
【MATLAB】求解非线性规划
【编程强训2】排序子序列+倒置字符串
Webapck packaging principle -- Analysis of startup process
【图像处理】图像直方图均衡化系统含GUI界面
C语言实现【三子棋游戏】(步骤分析和实现源码)
Rclone access web interface
【LINGO】求七个城市最小连线图,使天然气管道价格最低
Is it safe to do fund fixed investment on Great Wall Securities?
Illusory and simple screen raindrop post-processing effect
Understanding of Turing test and Chinese Room
How to create an exclusive vs Code theme
STM32F1与STM32CubeIDE编程实例-NEC协议红外接收与解码
Système de gestion de l'exploitation et de l'entretien, expérience d'exploitation humanisée
