当前位置:网站首页>Browser local storage webstroage
Browser local storage webstroage
2022-07-24 19:53:00 【_ ocean】
List of articles
localStorage The local store
localStroage The data of needs to be deleted manually , Even if you close the browser, the data will not disappear , When will you delete it , When does the data disappear
Delete manually : Such as clearing the cache
localStorage Of 4 Operations : Store the data 、 get data 、 Delete data 、 Empty data
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h2>localStorage</h2>
<button onclick="saveData()"> Click me to save a data </button>
<button onclick="readData()"> Click me to read a data </button>
<button onclick="deleteData()"> Click me to delete a data </button>
<button onclick="deleteAllData()"> Click I clear all data </button>
<script type='text/javascript'> function saveData (){
// localStorage The keys and values stored in are of string type , // Even if the incoming is number type , It will also be converted to string type , Default call toString Method // So if you want to pass the object, you can call JSON.stringify Method , Convert the object to json character string (json Strings can display the contents of objects , But the essence is also a string ) const p ={
id:"123",name:'yang',age:18} localStorage.setItem('msg','hello') localStorage.setItem('hello',123) localStorage.setItem('person',JSON.stringify(p)) } function readData (){
console.log(localStorage.getItem('msg')) console.log(localStorage.getItem('hello')) // JSON.parse(null) The result is still null console.log(JSON.parse(localStorage.getItem('person'))) } function deleteData (){
localStorage.removeItem('msg') } function deleteAllData (){
localStorage.clear() } </script>
</body>
</html>
localStorage View :
sessionStorage
sessionStorage As soon as the data browser in is closed
sessionStorage Of 4 Operations : Store the data 、 get data 、 Delete data 、 Empty data ( and localStroage equally )
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h2>sessionStorage</h2>
<button onclick="saveData()"> Click me to save a data </button>
<button onclick="readData()"> Click me to read a data </button>
<button onclick="deleteData()"> Click me to delete a data </button>
<button onclick="deleteAllData()"> Click I clear all data </button>
<script type='text/javascript'> function saveData (){
// sessionStorage The keys and values stored in are of string type , // Even if the incoming is number type , It will also be converted to string type , Default call toString Method // So if you want to pass the object, you can call JSON.stringify Method , Convert the object to json character string (json Strings can display the contents of objects , But the essence is also a string ) const p ={
id:"123",name:'yang',age:18} sessionStorage.setItem('msg','hello') sessionStorage.setItem('hello',123) sessionStorage.setItem('person',JSON.stringify(p)) } function readData (){
console.log(sessionStorage.getItem('msg')) console.log(sessionStorage.getItem('hello')) // JSON.parse(null) The result is still null console.log(JSON.parse(sessionStorage.getItem('person'))) } function deleteData (){
sessionStorage.removeItem('msg') } function deleteAllData (){
sessionStorage.clear() } </script>
</body>
</html>
View the data :
webstroage
webstroage when localStorage and sessionStorage Is called .
- The storage content size generally supports 5MB about ( Different browsers may be different )
- Browser side through
Window.sessionStorageandWindow.localStorageProperty to implement the local storage mechanism . SessionStorageThe stored content will disappear as the browser window closes .LocalStorageStored content , It needs to be cleared manually to disappear .
Two dots
- xxxxxStorage.getItem(xxx) If xxx Corresponding value Can't get , that getltem The return value of
null - JSON. parse(null) The result is still
null.
边栏推荐
- 微服务架构 | 服务监控与隔离 - [Sentinel] TBC...
- Sword finger offer 47. the maximum value of gifts
- Mysql8.0 learning record 19 - Page segments and tablespaces
- Introduction to fastdfs high availability
- Hide the middle digit of ID number
- How to use the interface control telerik UI for WinForms development step progress bar?
- Sword finger offer 42. maximum sum of continuous subarrays
- Leetcode652 finding duplicate subtrees
- Redis common configuration description
- 871. Sum of divisors
猜你喜欢

day 2

Clion configuring WSL tool chain

Analysis and Simulation of strlen function

Day 10 (inheritance, rewriting and use of super)

Interface component devaxpress asp Net v22.1 - new office 365 dark theme

Database index: index is not a panacea

Duilib actual combat 1- imitate Baidu online disk login interface

聊下自己转型测试开发的历程

Flink window & time principle

LSTM and Gru of RNN_ Attention mechanism
随机推荐
ATL container - catlmap, crbmap
从服务器批量下载文件到本地
Leetcode652 finding duplicate subtrees
Install SSL Certificate in Litespeed web server
Mysql8.0 learning record 19 - Page segments and tablespaces
Recursion of function [easy to understand]
Modbus communication protocol specification (Chinese) sharing
Clion configuring WSL tool chain
Getaverse,走向Web3的远方桥梁
【校招面经】8道指针面试真题,快来检测自己掌握了几道。
How to export map files tutorial
Sword finger offer 40. minimum number of K
02 | 环境准备:如何在windows下安装和配置一个基本的php开发环境?
Talk about your transformation test development process
Sword finger offer 48. the longest substring without repeated characters
The ark compiler is coming. What about APK reinforcement
Hide the middle digit of ID number
Detailed explanation of ELF format (I)
Mysql8.0 learning record 20 - trigger
Anaconda installs labelimg (super simple and available)