当前位置:网站首页>Web APLS phase - Section 14 - local storage
Web APLS phase - Section 14 - local storage
2022-06-27 20:01:00 【123come on ^o^】
List of articles
Learning goals
- Be able to write sessionStorage Data storage and acquisition
- Be able to write localStorage Data storage and acquisition
- Be able to tell the difference between them
1. The local store
With the rapid development of Internet , Web based applications are becoming more and more popular , And it's getting more and more complicated , In order to meet all kinds of needs , A large amount of data will be stored locally frequently ,HTML5 The specification puts forward relevant solutions .
Local storage features
1、 The data is stored in User browser in
2、 Set up 、 Easy to read 、 Even page refresh does not lose data
3、 Large capacity ,sessionStorage about 5M、localStorage about 20M
4、 Only strings can be stored , You can put objects JSON.stringify() Store after encoding
2. window.sessionStorage
1、 The life cycle is Close the browser window
2、 In the same window ( page ) Next, data can be shared
3. In the form of key value pairs Storage use
Store the data :
sessionStorage.setItem(key, value)
get data :
sessionStorage.getItem(key)
Delete data :
sessionStorage.removeItem(key)
Delete all data :
sessionStorage.clear()
<!DOCTYPE html>
<html lang="en">
<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>
<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"> Delete all data </button>
<script> //1. Get elements 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'); 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('pwd',val); }) get.addEventListener('click',function(){
// When we click , You can get the values in the form console.log(sessionStorage.getItem('uname')); }) remove.addEventListener('click',function(){
// When we click , You can delete the values in the form sessionStorage.removeItem('uname'); }) del.addEventListener('click', function() {
// When we click , Clear away all sessionStorage.clear(); }); </script>
</body>
</html>
Running results :
3. window.localStorage
- The declaration cycle is permanent , Unless you delete , Otherwise, closing the page will also exist
- You can have multiple windows ( page ) share ( The same browser can share )
- Store and use in the form of key value pairs
Store the data :
localStorage.setItem(key, value)
get data :
localStorage.getItem(key)
Delete data :
localStorage.removeItem(key)
Delete all data :
localStorage.clear()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<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'); set.addEventListener('click', function() {
var val = ipt.value; localStorage.setItem('username', val); }) get.addEventListener('click', function() {
console.log(localStorage.getItem('username')); }) remove.addEventListener('click', function() {
localStorage.removeItem('username'); }) del.addEventListener('click', function() {
localStorage.clear(); }) </script>
</body>
</html>
Running results :
3 Case study : Remember the user name
If you check remember user name , Next time the user opens the browser , The last login user name will be displayed automatically in the text box
case analysis
- Save the data , Use local storage
- Close page , You can also display the user name , So we use localStorage
- Open the page , First determine whether there is this user name , If there is , Just show the user name in the form , And check the box
- When the check box changes change event
- If you check the , Just store , Otherwise remove
<!DOCTYPE html>
<html lang="en">
<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>
<input type="text" id="username">
<input type="checkbox" name="" id="remember"> Remember the user name
<script> var username=document.querySelector('#username'); var remember=document.querySelector('#remember'); if(localStorage.getItem('username')){
username.value=localStorage.getItem('username'); remember.checked=true; } remember.addEventListener('change',function(){
if(this.checked){
localStorage.setItem('username',username.value) }else{
localStorage.removeItem('username'); } }) </script>
</body>
</html>
边栏推荐
- 经纬度分析
- What is ssr/ssg/isr? How do I host them on AWS?
- Doctoral Dissertation of the University of Toronto - training efficiency and robustness in deep learning
- [bug] Lenovo Xiaoxin has a problem. Your pin is unavailable.
- DCC888 :Register Allocation
- Bit. Store: long bear market, stable stacking products may become the main theme
- Golang map 并发读写问题源码分析
- Redis集群
- 什么是SSR/SSG/ISR?如何在AWS上托管它们?
- binder hwbinder vndbinder
猜你喜欢

Observable, reliable: the first shot of cloudops series Salon of cloud automation operation and maintenance

Doctoral Dissertation of the University of Toronto - training efficiency and robustness in deep learning

UE4实现长按功能

数据库引擎

《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

Redis集群

Crontab's learning essays

Data intelligence enters the "deep water area", and data governance is the key

散列表(Hash)-复习
随机推荐
429-二叉树(108. 将有序数组转换为二叉搜索树、538. 把二叉搜索树转换为累加树、 106.从中序与后序遍历序列构造二叉树、235. 二叉搜索树的最近公共祖先)
1027 Colors in Mars
1029 Median
1025 PAT Ranking
Batch insert data using MySQL bulkloader
UE4 realizes long press function
《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践
Crontab's learning essays
Doctoral Dissertation of the University of Toronto - training efficiency and robustness in deep learning
1024 Palindromic Number
数智化进入“深水区”,数据治理是关键
Leetcode 989. 数组形式的整数加法(简单)
【bug】上传图片出现错误(413 Request Entity Too Large)
Erreur Keil de Huada Single Chip Computer La solution de Weak
键入网址到网页显示,期间发生了什么?
数智化进入“深水区”,数据治理是关键
【bug】联想小新出现问题,你的PIN不可用。
数组练习 后续补充
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
指针和结构体