当前位置:网站首页>Browser local storage
Browser local storage
2022-07-01 07:19:00 【18 hate programming】
List of articles
Scene Introduction
First, we can understand what is the local storage of browsers through a scenario
For example, we open Taobao Search psp( Without logging in to your account ), Then close the website , We reopen Taobao , It can be found in the search history psp.
Why is it like this ? Here is the local storage of the website with the help of the browser , The data is stored on the hard disk of the computer .
We can use the developer tools of the browser to view :

Similarly, we can store things in the browser !
Browser local storage
Web Storage API Provides sessionStorage ( Session storage ) and localStorage( The local store ) Two storage objects are used to add the data of the web page 、 Delete 、 modify 、 Query operation .
- localStorage For long-term preservation of data across the site , Saved data has no expiration time , Until removed by hand .( If you close the browser, it won't disappear )
- sessionStorage Used to temporarily save the same window ( Or tabs ) The data of , The data will be deleted after closing the window or tab .
localStorage There are only two ways to delete data :
① Programmers use related cleanup methods
② The user manually deletes the cached data of the browser
Corresponding API And attributes :
window.localStorage
grammar :
window.localStorage
Save data syntax :
localStorage.setItem("key", "value");
Key value pairs will eventually be stored as strings
Read data syntax :
var lastname = localStorage.getItem("key");
Delete data syntax :
localStorage.removeItem("key");
Clear data syntax :
localStorage.clear();
We now create a web page and save some data on it for a long time :
<!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()"> Click on me to store data on the web page </button>
</body>
<script> function storeData() {
localStorage.setItem(' Zhang San ','18') } </script>
</html>

If we want to store an object :
<body>
<button onclick="storeData()"> Click on me to store data on the web page </button>
</body>
<script> let person = {
name:'Tom',age:'20'} function storeData() {
localStorage.setItem(' Zhang San ',6666) localStorage.setItem('msg',JSON.stringify(person)) } </script>

Here are some api Use it all :
<body>
<button onclick="storeData()"> Click on me to store data on the web page </button>
<button onclick="watchData()"> Click on my data on the web page </button>
<button onclick="deleteData()"> Click on me to delete data on the web page </button>
<button onclick="clearData()"> Click on me to clear the data on the web page </button>
</body>
<script> let person = {
name:'Tom',age:'20'} function storeData() {
localStorage.setItem(' Zhang San ',6666) localStorage.setItem('msg',JSON.stringify(person)) } function watchData() {
console.log(localStorage.getItem(' Zhang San ')) console.log(JSON.parse(localStorage.getItem('msg'))) } function deleteData() {
localStorage.removeItem(' Zhang San ') } function clearData() {
localStorage.clear() } </script>
Objects will be displayed as strings if viewed directly , Obviously this is not what we want , We can use JSON.parse Analyze , We can get the object we want again .
If you view non-existent data , Will be displayed null.
JSON.parse(null) The result is still null
window.sessionStorage
And window.localStorage The method of use is exactly the same
Difference is that :sessionStorage Used to temporarily save the same window ( Or tabs ) The data of , The data will be deleted after closing the window or tab .
summary
webStorage
The storage content size generally supports 5MB about ( Different browsers may be different )
Browser side through Window.sessionStorage and Window.localStorage Property to implement the local storage mechanism .
relevant API:
xxxxxStorage.setItem('key', 'value');
This method takes a key and a value as arguments , Key value pairs are added to the store , If key name exists , Then update its corresponding value .xxxxxStorage.getItem('person'); This method takes a key name as an argument , Return the value corresponding to the key name .
xxxxxStorage.removeItem('key'); This method takes a key name as an argument , And remove the key name from the storage .
xxxxxStorage.clear() This method will empty all data in the storage .
remarks :
- SessionStorage The stored content will disappear as the browser window closes .
- LocalStorage Stored content , It needs to be cleared manually to disappear .
xxxxxStorage.getItem(xxx)If xxx Corresponding value Can't get , that getItem The return value of null.JSON.parse(null)The result is still null.
边栏推荐
- Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list
- 為什麼這麼多人轉行產品經理?產品經理發展前景如何?
- 灰度何以跌下神坛?
- C language implementation [Sanzi chess game] (step analysis and implementation source code)
- 【编程强训2】排序子序列+倒置字符串
- [recommendation technology] matlab simulation of network information recommendation technology based on collaborative filtering
- vscode 根据 ESLint 规范自动格式化代码
- Programming examples of stm32f1 and stm32subeide infrared receiving and decoding of NEC protocol
- Rclone Chinese document: a collection of common commands
- [the path of system analysts] Chapter 5: software engineering of double disk (reverse clean room and Model Driven Development)
猜你喜欢

【LINGO】求解二次规划

ctfshow-web351(SSRF)

如何进入互联网行业,成为产品经理?没有项目经验如何转行当上产品经理?

為什麼這麼多人轉行產品經理?產品經理發展前景如何?

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

Solve the problem of "unexpected status code 503 service unavailable" when kaniko pushes the image to harbor

redisson使用全解——redisson官方文档+注释(上篇)
![[network planning] (I) hub, bridge, switch, router and other concepts](/img/7b/fcef37496517c854ac1dbfb35fa3f4.png)
[network planning] (I) hub, bridge, switch, router and other concepts
![[Electrical dielectric number] electrical dielectric number and calculation considering HVDC and facts components](/img/7c/2b1d4797f367cced51f36e8a1bb199.png)
[Electrical dielectric number] electrical dielectric number and calculation considering HVDC and facts components

ctfhub-端口扫描(SSRF)
随机推荐
How to choose a product manager course when changing to a product manager?
Jax's deep learning and scientific computing
Image style migration cyclegan principle
[Electrical dielectric number] electrical dielectric number and calculation considering HVDC and facts components
盘点华为云GaussDB(for Redis)六大秒级能力
ctfshow-web355,356(SSRF)
Which securities company is better or safer for mobile phone account opening
【FPGA帧差】基于VmodCAM摄像头的帧差法目标跟踪FPGA实现
[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
Will Internet talents be scarce in the future? Which technology directions are popular?
在长城证券上做基金定投安全吗?
【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
未来互联网人才还稀缺吗?哪些技术方向热门?
Is it reliable to open an account on the compass with your mobile phone? Is there any potential safety hazard
DC-4靶机
Vscode automatically formats code according to eslint specification
ctfhub-端口扫描(SSRF)
赌上了绩效,赢了公司CTO,我要搭DevOps平台!
ctfshow-web352,353(SSRF)
比赛即实战!中国软件杯发布全新产业创新赛项,校企可联合参赛
