当前位置:网站首页>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
边栏推荐
- Android engineers, how to use kotlin to provide productivity?
- 【嵌入式C基础】第2篇:进制转换与BCD编码
- Phpstudy steps to quickly build a website (teach you to build it by hand)
- Connected Block & food chain - (summary of parallel search set)
- Vs code is not in its original position after being updated
- 03 pyechars rectangular coordinate system chart (example code + effect drawing)
- [Bi design teaching] STM32 and FreeRTOS realize low power consumption
- Which big model is better? Openbmb releases bmlist to give you the answer!
- [embedded C foundation] Part 3: constants and variables
- UV germicidal lamp chip dlt8p65sa Jericho
猜你喜欢

企业数字化本质

Redefinition problem of defining int i variable in C for loop

Automatic light sensing arm lamp touch chip-dlt8sa15b-jericho

Flexpro software: measurement data analysis in production, research and development

Bear market spread portfolio

Transaction of MySQL underlying principle (2)

Brother bird talks about cloud native security best practices

RGB game atmosphere light touch chip-dlt8s04a-jericho

Black cat takes you to learn EMMC Protocol Part 26: hardware reset operation of EMMC (h/w reset)

【C语言易错点】第4篇:结构体在内存中存储规则详讲
随机推荐
Brother bird talks about cloud native security best practices
Black cat takes you to learn EMMC Protocol Part 24: detailed explanation of EMMC bus test program (cmd19 & cmd14)
CTO of youhaoda, MVP of Huawei cloud, and Zhang Shanyou: build cloud native applications based on kubernetes and dapr
Code layered management of interface testing based on RF framework
机器学习基础-支持向量机 SVM-17
Transaction of MySQL underlying principle (2)
How does kotlin help you avoid memory leaks?
[embedded C foundation] Part 7: detailed introduction to C language process control
Leetcode daily question (2196. create binary tree from descriptions)
Comments are not allowed in JSON
03 pyechars rectangular coordinate system chart (example code + effect drawing)
BiliBili Yang Zhou: above efficiency, efficient delivery
如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南
.net for subtraction, intersection and union of complex type sets
黑猫带你学eMMC协议第24篇:eMMC的总线测试程序详解(CMD19 & CMD14)
Android工程师,如何使用Kotlin提供生产力?
Call / put option price curve
机器学习实战-决策树-22
Intrinsic value and time value of options
Connected Block & food chain - (summary of parallel search set)