当前位置:网站首页>浏览器存储WebStorage
浏览器存储WebStorage
2022-08-05 05:22:00 【-小龙人】
介绍
浏览器存储WebStorage包括 LocalStorage 和 SessionStorage,存储内容大小一般在5M左右。
如何使用
浏览器通过Window.localStorage 和 Window.sessionStorage属性来实现本地存储机制,在实际使用中可省略Window。
API使用
LocalStorage 和 SessionStorage的API方法名称用法相同,分别有新增、获取、删除、清空四个方法,方法名称及使用方法见下面示例:
//新增WebStorage: setItem(key,val)
localStorage.setItem("a", "123");
localStorage.setItem("b", "456");
localStorage.setItem("c", "789");
sessionStorage.setItem("a", "123");
sessionStorage.setItem("b", "456");
sessionStorage.setItem("c", "789");
//获取WebStorage: getItem(key)
localStorage.getItem("a");
sessionStorage.getItem("a");
//删除WebStorage: removeItem(key)
localStorage.removeItem("c");
sessionStorage.removeItem("c");
//清空WebStorage: removeItem(key)
localStorage.clear();
sessionStorage.clear();
LocalStorage 和 SessionStorage区别
同为浏览器储存的WebStorage,LocalStorage 和 SessionStorage使用的API相同,但实际使用场景是有区别的:
LocalStorage
使用localStorage保存的数据会长期保留,即使你关闭浏览器下次重新打开该页面,保存的数据依然还在不会被清空,如果需要清空必须手动清除。而且在同个浏览器相同源(window.location.origin)的其他页面都是可以通过 localStorage 访问到,当然了源与源之间的 localStorage 是不能互相访问的。即每个源之间的 localStorage 是相互独立的。SessionStorage
使用sessionStorage保存的数据只是当前页面会保留,即页面会话级别,当关闭浏览器或者关闭页签后sessionStorage保存的数据会被清空,而且在同个浏览器即使相同源,其他页面不可以共享sessionStorage保存的数据。
边栏推荐
- VRRP概述及实验
- vim的三种模式
- flink cdc 目前支持Gauss数据库源吗
- One-arm routing and 30% switch
- [Day1] (Super detailed steps) Build a soft RAID disk array
- NIO works is analysed
- The problem of redirecting to the home page when visiting a new page in dsf5.0
- Getting Started 05 Using cb() to indicate that the current task is complete
- 618, you may be able to wake up a little bit
- 【Machine Learning】1 Univariate Linear Regression
猜你喜欢
随机推荐
Configuration of TensorFlow ObjecDetectionAPI under Anaconda3 of win10 system
markdown editor template
LinkSLA insists that users come first and creates a sustainable operation and maintenance service plan
Mongodb查询分析器解析
I217-V network disconnection problem in large traffic under openwrt soft routing
Account and Permission Management
正则表达式小实例--验证邮箱地址
flink cdc 目前支持Gauss数据库源吗
What are some things that you only know when you do operation and maintenance?
spark operator-textFile operator
idea 常用快捷键
有哪些事情是你做了运维才知道的?
Insight into the general trend of the Internet, after reading this article, you will have a thorough understanding of Chinese domain names
入门文档05 使用cb()指示当前任务已完成
Switch principle
硬盘分区和永久挂载
Getting Started 03 Distinguish between development and production environments ("hot update" is performed only in the production environment)
[Day5] Soft and hard links File storage, deletion, directory management commands
NIO works is analysed
传输层协议(TCP3次握手)