当前位置:网站首页>The difference between sessionstorage, localstorage and cookies
The difference between sessionstorage, localstorage and cookies
2022-07-28 13:13:00 【Pig Xiaoyong】
Use scenarios 1:
Same website , After successful login, a company will be saved id, Used to judge page style .
Put it in session There is a problem in the middle : When login succeeds, copy the URL and open ,session disappear , Page style error .
programme : Use localStorage Storage .
Use scenarios 2:
In a project , Yes 3 Website ,3 Login with different websites , But all interfaces need to be transmitted token, Common fields such as .
programme : Use session Store their own information
The process :
Try first localStorage Storage , Found two browser tabs , The result of saving is the same .
And then I tried session, The discovery is different , Each stores its own information , It's what I want .
principle :
sessionStorage、localStorage and cookie The difference between
Common ground : It's all saved on the browser side 、 And homologous
difference :
1、cookie Data is always in the same source http Carry... In the request ( Even if you don't need ), namely cookie Pass it back and forth between the browser and the server , and sessionStorage and localStorage It doesn't automatically send data to the server , Save locally only .cookie There are paths to data (path) The concept of , Can restrict cookie Only belong to a certain path
2、 Storage size limits are different ,cookie Data cannot exceed 4K, And because every time http Request will be carried cookie、 therefore cookie Only suitable for keeping small data , Such as session ID .sessionStorage and localStorage Although there are also storage size limitations , But more than cookie Much more , You can achieve 5M Or bigger
3、 Data validity is different ,sessionStorage: Valid only until the current browser window is closed ;localStorage: Always effective , Windows or browsers are closed and saved all the time , So as persistent data ;cookie: Only in settings cookie Valid before expiration time , Even if the window is closed or the browser is closed
4、 Different scopes ,sessionStorage Don't share in different browser windows , Even if it's the same page ;localstorage It's shared in all the same source windows ;cookie It is also shared in all homologous windows
5、web Storage Support event notification mechanism , You can send a data update notification to the listener
6、web Storage Of api The interface is more convenient to use
sessionStorage And page js The difference between data objects
The general js Object's lifetime is only valid for the current page , So in the case of reloading a page by refreshing the page or going to another page , The data doesn't exist
and sessionStorage As long as the same source is in the same window , Refresh the page or go to different pages of the same origin , Data always exists , That is to say, as long as the browser is not closed , The data still exists
边栏推荐
- [FPGA]: MATLAB generates COE files
- Force buckle 315 calculates the number of elements smaller than the current element on the right
- [embedded C foundation] Part 8: explanation of C language array
- Remove the plug-in of category in WordPress link
- [embedded C foundation] Part 5: original code / inverse code / complement code
- [graduation design teaching] ultrasonic ranging system based on single chip microcomputer - Internet of things embedded stm32
- Sub thread update UI full solution
- Can molecular modeling solve the data dilemma of AI pharmacy?
- Solution to using json.tojsonstring to display question marks in Chinese in Servlet
- Aragon创建DAO polygon BSC测试网
猜你喜欢

Definition of option basis

How to improve deep learning performance?

机器学习基础-决策树-12

LeetCode·每日一题·1331.数组序号转换·离散化

SSM框架网上书城全套

Original juice multifunctional Juicer touch chip-dlt8t02s-jericho
![[matlab]: FFT related problems](/img/08/c721394c172bdfd1d5d55aef31ccbf.png)
[matlab]: FFT related problems

黑猫带你学eMMC协议第24篇:eMMC的总线测试程序详解(CMD19 & CMD14)

Jetpack Compose 完全脱离 View 系统了吗?

The essence of enterprise Digitalization
随机推荐
Chapter IX rest Service Security
Stepless dimming colorful RGB mirror light touch chip-dlt8s12a-jericho
.NET的求复杂类型集合的差集、交集、并集
[FPGA]: ise and Modelsim joint simulation
【嵌入式C基础】第9篇:C语言指针的基本用法
Automatic light sensing arm lamp touch chip-dlt8sa15b-jericho
[June 28 event preview] low code Summit
Machine learning practice - integrated learning-23
【嵌入式C基础】第7篇:C语言流程控制详讲
FutureWarning: Indexing with multiple keys (implicitly converted to a tuple of keys) will be depreca
How to add PDF virtual printer in win11
机器学习实战-集成学习-23
Is jetpack compose completely out of view?
什么是事务及数据库的优化方法
机器学习基础-决策树-12
Le transaction
【嵌入式C基础】第4篇:运算符的使用
Fundamentals of machine learning - support vector machine svm-17
子线程更新UI全解
[embedded C foundation] Part 6: super detailed explanation of common input and output functions