当前位置:网站首页>JS local storage
JS local storage
2022-06-24 18:36:00 【Brother Mengfan】
Catalog
2.1、sessionStorage characteristic
2.2、 Store the data sessionStorage.setItem(key, value)
2.3、 get data sessionStorage.getItem(key)
2.4、 Delete a data sessionStorage.removeItem(key)
2.5、 Delete all data sessionStorage.clear()
3.1、localStorage characteristic
3.2、 Store the data localStorage.setItem(key, value)
3.3、 get data localStorage.getItem(key)
3.4、 Delete a data localStorage.removeItem(key)
3.5、 Delete all data localStorage.clear()
JS The local store
One 、 Local storage features
Two 、sessionStorage
2.1、sessionStorage characteristic
(1) The lifecycle is to close the browser window
(2) In the same window ( page ) Next, data can be shared
(3) Store and use in the form of key value pairs
Method :

2.2、 Store the data sessionStorage.setItem(key, value)
The code is as follows :
<input type="text">
<button class="set"> Store the data </button>
<button class="get"> get data </button>
<button class="remove"> Delete data </button>
<button class="del"> Clear all data </button>
<script>
var ipt = document.querySelector('input');
var set = document.querySelector('.set')
var get = document.querySelector('.get')
// Store the data
set.addEventListener('click', function() {
// When we click , You can store the values in the form
var val = ipt.value;
sessionStorage.setItem('uname', val);
})
</script>
Running results :

2.3、 get data sessionStorage.getItem(key)
The code is as follows :
<input type="text">
<button class="set"> Store the data </button>
<button class="get"> get data </button>
<button class="remove"> Delete data </button>
<button class="del"> Clear all data </button>
<script>
var ipt = document.querySelector('input');
var set = document.querySelector('.set')
var get = document.querySelector('.get')
// Store the data
set.addEventListener('click', function() {
// When we click , You can store the values in the form
var val = ipt.value;
sessionStorage.setItem('uname', val);
})
// get data
get.addEventListener('click', function() {
// When we click , You can get the data
console.log(sessionStorage.getItem('uname'));
})
</script>Running results :

2.4、 Delete a data sessionStorage.removeItem(key)
The code is as follows :
<input type="text">
<button class="set"> Store the data </button>
<button class="get"> get data </button>
<button class="remove"> Delete data </button>
<button class="del"> Clear all data </button>
<script>
var ipt = document.querySelector('input');
var set = document.querySelector('.set')
var get = document.querySelector('.get')
var remove = document.querySelector('.remove')
// Store the data
set.addEventListener('click', function() {
// When we click , You can store the values in the form
var val = ipt.value;
sessionStorage.setItem('uname', val);
sessionStorage.setItem('pws', val);
})
// get data
get.addEventListener('click', function() {
// When we click , You can get the data
console.log(sessionStorage.getItem('uname'));
})
// Delete data
remove.addEventListener('click', function() {
// When we click , You can delete the data one by one
sessionStorage.removeItem('uname');
})
</script>Running results :
Store two data , Delete only one

2.5、 Delete all data sessionStorage.clear()
<input type="text">
<button class="set"> Store the data </button>
<button class="get"> get data </button>
<button class="remove"> Delete data </button>
<button class="del"> Clear all data </button>
<script>
var ipt = document.querySelector('input');
var set = document.querySelector('.set')
var get = document.querySelector('.get')
var remove = document.querySelector('.remove')
var del = document.querySelector('.del');
// Store the data
set.addEventListener('click', function() {
// When we click , You can store the values in the form
var val = ipt.value;
sessionStorage.setItem('uname', val);
sessionStorage.setItem('pws', val);
})
// get data
get.addEventListener('click', function() {
// When we click , You can get the data
console.log(sessionStorage.getItem('uname'));
})
// Delete a data
remove.addEventListener('click', function() {
// When we click , You can delete the data one by one
sessionStorage.removeItem('uname');
})
// Delete all data
del.addEventListener('click', function() {
// When we click , You can delete all the data
sessionStorage.clear();
})
</script>3、 ... and 、localStorage
3.1、localStorage characteristic
(1) The declaration cycle is permanent , Unless you delete it manually, the closed page will also exist
(2) You can have multiple windows ( page ) share ( The same browser can share )
(3) Store and use in the form of key value pairs
Method :

3.2、 Store the data localStorage.setItem(key, value)
The code is as follows :
<input type="text">
<button class="set"> Store the data </button>
<button class="get"> get data </button>
<button class="remove"> Delete data </button>
<button class="del"> Clear all data </button>
<script>
var ipt = document.querySelector('input');
var set = document.querySelector('.set')
var get = document.querySelector('.get')
var remove = document.querySelector('.remove')
var del = document.querySelector('.del');
// Store the data
set.addEventListener('click', function() {
// When we click , You can store the values in the form
var val = ipt.value;
localStorage.setItem('uname', val);
})
</script>Running results ;

3.3、 get data localStorage.getItem(key)
<input type="text">
<button class="set"> Store the data </button>
<button class="get"> get data </button>
<button class="remove"> Delete data </button>
<button class="del"> Clear all data </button>
<script>
var ipt = document.querySelector('input');
var set = document.querySelector('.set')
var get = document.querySelector('.get')
var remove = document.querySelector('.remove')
var del = document.querySelector('.del');
// Store the data
set.addEventListener('click', function() {
// When we click , You can store the values in the form
var val = ipt.value;
localStorage.setItem('uname', val);
})
// get data
get.addEventListener('click', function() {
// When we click , You can get the data
console.log(localStorage.getItem('uname'));
})
</script>3.4、 Delete a data localStorage.removeItem(key)
<input type="text">
<button class="set"> Store the data </button>
<button class="get"> get data </button>
<button class="remove"> Delete data </button>
<button class="del"> Clear all data </button>
<script>
var ipt = document.querySelector('input');
var set = document.querySelector('.set')
var get = document.querySelector('.get')
var remove = document.querySelector('.remove')
var del = document.querySelector('.del');
// Store the data
set.addEventListener('click', function() {
// When we click , You can store the values in the form
var val = ipt.value;
localStorage.setItem('uname', val);
})
// get data
get.addEventListener('click', function() {
// When we click , You can get the data
console.log(localStorage.getItem('uname'));
})
// Delete a data
remove.addEventListener('click', function() {
// When we click , You can delete the data one by one
localStorage.removeItem('uname');
})
</script>3.5、 Delete all data localStorage.clear()
<input type="text">
<button class="set"> Store the data </button>
<button class="get"> get data </button>
<button class="remove"> Delete data </button>
<button class="del"> Clear all data </button>
<script>
var ipt = document.querySelector('input');
var set = document.querySelector('.set')
var get = document.querySelector('.get')
var remove = document.querySelector('.remove')
var del = document.querySelector('.del');
// Store the data
set.addEventListener('click', function() {
// When we click , You can store the values in the form
var val = ipt.value;
localStorage.setItem('uname', val);
})
// get data
get.addEventListener('click', function() {
// When we click , You can get the data
console.log(localStorage.getItem('uname'));
})
// Delete a data
remove.addEventListener('click', function() {
// When we click , You can delete the data one by one
localStorage.removeItem('uname');
})
// Delete all data
del.addEventListener('click', function() {
// When we click , You can delete all the data
localStorage.clear();
})
</script>Four 、 Remember the user name

demand :
If you check remember user name , Next time the user opens the browser , The last login user name will be automatically displayed in the text box
case analysis :
(1) Store the data , Use local storage ;
(2) Close page , You can also display the user name , So we use localStorage;
(3) Open the page , First determine whether there is this user name , If yes, the user name will be displayed in the form , And check the box ;
(4) When the check box changes change event ;
(5) If you check the , Just store , Otherwise remove .
Code :
<input type="text" id="username">
<input type="checkbox" id="remeber"> Remember the user name
<script>
var username = document.querySelector('#username');
var remeber = document.querySelector('#remeber');
// Judge if there is any data
if (localStorage.getItem('username')) {
// Take if there is
username.value = localStorage.getItem('username');
// Also check the checkbox
remeber.checked = true;
}
remeber.addEventListener('change', function() {
if (this.checked) {
localStorage.setItem('username', username.value)
} else {
localStorage.removeItem('username');
}
})
</script>Running results ;

边栏推荐
- Digital trend analysis of B2B e-commerce market mode and trading capacity in electronic components industry
- Exception: Gradle task assembleDebug failed with exit code 1
- Selection (032) - what is the output of the following code?
- Top ten popular codeless testing tools
- Analysis on the issue of raising the right of MSSQL in 2021 secondary vocational group network security competition in Shandong Province
- Flex box flex attribute
- 110. balanced binary tree
- Tencent cloud TCS: an application-oriented one-stop PAAS platform
- Redis series (3) - sentry highly available
- SAP license: what is ERP supply chain
猜你喜欢

Six configuration management tools that administrators must know

Graph traversal (BFS and DFS) C language pure handwriting

Two micro service interviews where small companies suffer losses

Crmeb multi merchant PC packaging tutorial

Specification for self test requirements of program developers

国家出手了!对知网启动网络安全审查

Several key points for enterprises to pay attention to digital transformation

Sudoku (easy to understand)

The country has made a move! Launch network security review on HowNet

Vite+web3:报错出现ReferenceError: process is not defined
随机推荐
Exception: Gradle task assembleDebug failed with exit code 1
What are the reasons for the abnormal playback of the online channel of the channel accessed by easycvr national standard protocol?
What is decision intelligence?
EasyNVR使用Onvif探测设备失败,显示“无数据”是什么原因?
SAP license: ERP for supply chain management and Implementation
解决执行MapReduce程序控制台没有日志信息WARN Please initialize the log4j system properly
电子元器件行业B2B电商市场模式、交易能力数字化趋势分析
Monotone stack template
Leetcode question 136 [single number]
Selection (032) - what is the output of the following code?
[North Asia data recovery]_ mdb_ catalog. Mongodb database data recovery case in case of WT file corruption
Several key points for enterprises to pay attention to digital transformation
Digital trend analysis of B2B e-commerce market mode and trading capacity in electronic components industry
Five skills of selecting embedded programming language
110. balanced binary tree
Selection (031) -cool_ How long can secret be accessed?
Skills of writing test cases efficiently
717.1-bit and 2-bit characters [sliding window]
Leetcode daily question solution: 717 1-bit and 2-bit characters - reverse order
How about China Power Investment Xianrong futures? Is it safe to open futures accounts?
