当前位置:网站首页>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.
边栏推荐
- Anaconda installs labelimg (super simple and available)
- Monotone stack and monotone queue (linear complexity optimization)
- Meshlab & PCL ISS key points
- Know typescript
- "Six pillars of self esteem" self esteem comes from one's own feelings
- [untitled]
- About the largeheap attribute
- [face to face experience of school recruitment] 8 real questions of pointer interview. Come and test how many you have mastered.
- Rotation matrix derivation process
- Richview table table alignment
猜你喜欢

01 | opening words: teach you to build a blog website hand in hand

Analysis and Simulation of strlen function

LSTM and Gru of RNN_ Attention mechanism
Siyuan notes V2.1.2 synchronization problem

Maya coffee machine modeling

Lights of thousands of families in the year of xinchou

02 | environment preparation: how to install and configure a basic PHP development environment under windows?

C # shelling tool for code encryption protection

Day 9 (this keyword and experiment)

Day 10 (inheritance, rewriting and use of super)
随机推荐
Unit DLU of resource editor
Hucang integrated release of full data value, sequoiadb V5.2 online conference heavy attack
How to view the execution plan of a stored procedure in Youxuan database
[understanding of opportunity-48]: Guiguzi - Chapter 7 - collect information in advance, make predictions and implementation plans in advance
【德味】安全:如何为行人提供更多保护
[untitled]
Prevent static decompilation, dynamic debugging and plug-in
Software core data protection solution
Hide the middle digit of ID number
Interface component devaxpress asp Net v22.1 - new office 365 dark theme
Recursion of function [easy to understand]
Decorator of function
Setting up a dual machine debugging environment for drive development (vs2017)
Clion configuring WSL tool chain
Description of large and small end mode
MySQL advanced
Duilib actual combat 1- imitate Baidu online disk login interface
Cmake series tutorial 2 HelloWorld
Know typescript
Original reverse compensation and size end