当前位置:网站首页>浏览器本地存储
浏览器本地存储
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。
边栏推荐
- Do securities account opening affect the security of account opening
- Are there any practical skills for operation and maintenance management
- Cadence OrCAD Capture “网络名”相同,但是未连接或连接错误的解放方案之nodename的用法
- 【LINGO】求无向图的最短路问题
- [target detection] yolov5, the shoulder of target detection (detailed principle + Training Guide)
- ctfshow-web355,356(SSRF)
- 华泰证券开户是安全可靠的么?怎么开华泰证券账户
- [lingo] find the shortest path problem of undirected graph
- Understanding of Turing test and Chinese Room
- rclone 访问web界面
猜你喜欢

Jax's deep learning and scientific computing

北漂程序员深夜emo发帖求助:女朋友走了我很孤独 ......
![[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array](/img/5e/068268857b8c7c2065ba552fd23dbb.png)
[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array

关于图灵测试和中文屋Chinese room的理解

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

(I) apple has open source, but so what?

How to permanently configure local opencv4.5.5 for vs2019

Is it suitable for girls to study product manager? What are the advantages?

Illusory and simple screen raindrop post-processing effect

盘点华为云GaussDB(for Redis)六大秒级能力
随机推荐
华泰证券开户是安全可靠的么?怎么开华泰证券账户
Rclone Chinese document: a collection of common commands
【分类模型】Q 型聚类分析
[FPGA frame difference] FPGA implementation of frame difference target tracking based on vmodcam camera
电脑有网络,但所有浏览器网页都打不开,是怎么回事?
广发证券开户是安全可靠的么?怎么开广发证券账户
[chapter 72 of the flutter problem series] a solution to the problem that pictures taken in the flutter using the camera plug-in are stretched
Summary of the concept and advantages of 5g massive MIMO
Mysql与Redis一致性解决方案
C# Newtonsoft.Json中JObject的使用
ctfshow-web351(SSRF)
MySQL and redis consistency solution
【剑指offer&牛客101】中那些高频笔试,面试题——链表篇
用手机在指南针上开户靠谱吗?这样有没有什么安全隐患
如何制作专属的VS Code主题
[Tikhonov] image super-resolution reconstruction based on Tikhonov regularization
Challenges faced by operation and maintenance? Intelligent operation and maintenance management system to help you
LeetCode+ 71 - 75
【图像处理】图像直方图均衡化系统含GUI界面
How to choose a product manager course when changing to a product manager?
