当前位置:网站首页>浏览器本地存储
浏览器本地存储
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。
边栏推荐
- We found a huge hole in MySQL: do not judge the number of rows affected by update!!!
- Image style migration cyclegan principle
- 继妹变继母,儿子与自己断绝关系,世界首富马斯克,为何这么惨?
- ctfshow-web352,353(SSRF)
- rclone常用子命令中文解释
- 【LINGO】求无向图的最短路问题
- 為什麼這麼多人轉行產品經理?產品經理發展前景如何?
- Rclone Chinese document: a collection of common commands
- 比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛
- C# 读写自定义的Config文件
猜你喜欢
Understanding of Turing test and Chinese Room
【图像处理】图像直方图均衡化系统含GUI界面
How to enter the Internet industry and become a product manager? How to become a product manager without project experience?
ctfshow-web351(SSRF)
1286_FreeRTOS的任务优先级设置实现分析
Jax's deep learning and scientific computing
C language implementation [minesweeping game] full version (implementation source code)
[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
C# Newtonsoft.Json中JObject的使用
ctfshow-web352,353(SSRF)
随机推荐
Jax's deep learning and scientific computing
【MATLAB】求解非线性规划
How to enter the Internet industry and become a product manager? How to become a product manager without project experience?
Easynvs cloud management platform function reconfiguration: support adding users, modifying information, etc
ctfshow-web354(SSRF)
微软宣布开源 (GODEL) 语言模型聊天机器人
Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
Summary of the concept and advantages of 5g massive MIMO
运维管理有什么实用的技巧吗
【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算
C language implementation [minesweeping game] full version (implementation source code)
【剑指offer&牛客101】中那些高频笔试,面试题——链表篇
Which securities company is better or safer for mobile phone account opening
ctfshow-web355,356(SSRF)
比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛
iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
LeetCode+ 71 - 75
[programming training] delete public characters (hash mapping) + team competition (greedy)
发现了一个 MySQL 的巨坑:update 更新别再用影响行数做判断了!!!
為什麼這麼多人轉行產品經理?產品經理發展前景如何?