interview-answe
interview-answe copied to clipboard
24.sessionStorage 、localStorage 和 cookie 之间的区别
共同点:用于浏览器端存储的缓存数据
不同点:
(1)、存储内容是否发送到服务器端:当设置了 Cookie 后,数据会发送到服
务器端,造成一定的宽带浪费;web storage,会将数据保存到本地,不会造成宽
带浪费;
(2)、数据存储大小不同:Cookie 数据不能超过 4K,适用于会话标识;web storage 数据存储可以达到 5M;
(3)、数据存储的有效期限不同:cookie 只在设置了 Cookid 过期时间之前一直
有效,即使关闭窗口或者浏览器; sessionStorage,仅在关闭浏览器之前有效;
localStorage,数据存储永久有效;
(4)、作用域不同:cookie 和 localStorage 是在同源同窗口中都是共享的;
sessionStorage 在不同的浏览器窗口中共享,即使是同一个页面。
Common denominator: cache data for browser-side storage
Difference:
(1) whether the stored content is sent to the server: when the Cookie is set, the data will be sent to the server Service terminal, causing a certain amount of broadband waste; Web storage, which stores data locally, does not create width With waste;
(2) different data storage sizes: Cookie data cannot exceed 4K, which is suitable for session identification; Web storage data storage can reach 5M;
(3) the validity period of data storage is different: the cookie is kept until the Cookid expiration time is set Effective, even if the window or browser is closed; SessionStorage, only valid until the browser is closed; LocalStorage, data storage is permanent;
(4) different scope: cookie and localStorage are Shared in the homologous window; SessionStorage is Shared in different browser Windows, even on the same page.
第4点有点问题把,localStorage 是同源的窗口都是共享的,sessionStorage是单个窗口的把