当前位置:网站首页>本地存储localStorage⽤法详解
本地存储localStorage⽤法详解
2022-07-17 01:32:00 【梦凝哲雪】
⼀、什么是localStorage?
在HTML5中,新加⼊了⼀个localStorage特性,这个特性主要是⽤来作为本地存储来使⽤的,解决了cookie存储空间不⾜的问题(cookie中每条cookie的存储空间为4k),localStorage中⼀般浏览器⽀持的是5M⼤⼩,这个在不同的浏览器中localStorage 会有所不同。
⼆、localStorage的优势与局限
localStorage的优势
1、localStorage拓展了cookie的4K限制
2、localStorage会可以将第⼀次请求的数据直接存储到本地,这个相当于⼀个5M⼤⼩的针对于前端页⾯的数据库,相⽐于cookie可以节约带宽,但是这个却是只有在⾼版本的浏览器中才⽀持的
localStorage的局限
1、浏览器的⼤⼩不统⼀,并且在IE8以上的IE版本才⽀持localStorage这个属性
2、⽬前所有的浏览器中都会把localStorage的值类型限定为string类型,这个在对我们⽇常⽐较常见的JSON对象类型需要⼀些转换
3、localStorage在浏览器的隐私模式下⾯是不可读取的
4、localStorage本质上是对字符串的读取,如果存储内容多的话会消耗内存空间,会导致页⾯变卡
5、localStorage不能被爬⾍抓取到
localStorage与sessionStorage的唯⼀⼀点区别就是localStorage属于永久性存储,⽽sessionStorage属于当会话结束的时候,sessionStorage中的键值对会被清空。
三、localStorage的使⽤
清空localStorage
localStorage.clear() // undefined
localStorage //Storage {length: 0} 存储数据
存储数据
localStorage.setItem(“name”,“caibin”) //存储名字为name值为caibin的变量
localStorage.name = “caibin”; // 等价于上⾯的命令
localStorage // Storage{name: “caibin”, length: 1} 读取数据
读取数据
localStorage.getItem(“name”) //caibin,读取保存在localStorage对象⾥名为name的变量的值
localStorage.name // “caibin”
localStorage.valueOf() //读取存储在localStorage上的所有数据
localStorage.key(0) // 读取第⼀条数据的变量名(键值)
//遍历并输出localStorage⾥存储的名字和值
for(var i=0; i<localStorage.length;i++){
console.log(‘localStorage⾥存储的第’+i+‘条数据的名字为:’+localStorage.key(i)+‘,值为:’+localStorage.getItem(localStorage.key(i)));
}
删除某个变量
localStorage.removeItem(“name”); //undefined
localStorage // Storage {length: 0} 可以看到之前保存的name变量已经从localStorage⾥删除了
检查localStorage⾥是否保存某个变量
// 这些数据都是测试的,是在我当下环境⾥的,只是demo哦~
localStorage.hasOwnProperty(‘name’) // true
localStorage.hasOwnProperty(‘sex’) // false
将数组转为本地字符串
var arr = [‘aa’,‘bb’,‘cc’]; // [“aa”,“bb”,“cc”]
localStorage.arr = arr //[“aa”,“bb”,“cc”]
localStorage.arr.toLocaleString(); // “aa,bb,cc”
将JSON存储到localStorage⾥
var students = {
xiaomin: {
name: “xiaoming”,
grade: 1
},
teemo: {
name: “teemo”,
grade: 3
}
}
students = JSON.stringify(students); //将JSON转为字符串存到变量⾥
console.log(students);
localStorage.setItem(“students”,students);//将变量存到localStorage⾥
var newStudents = localStorage.getItem(“students”);
newStudents = JSON.parse(students); //转为JSON
console.log(newStudents); // 打印出原先对象
边栏推荐
- Basic IDL content of note 1: common data types_ Create array_ Type conversion_ Print output_ Basic operation_ Relational operation
- 基于Matlab的男女声音信号分析与处理
- Go语言中的Iota关键字怎么使用
- Zabbix6.0 monitors Dell and IBM server hardware through Idrac and imm2
- Thinkphp5.0模型操作使用page进行分页
- S32k148evb about eNet loopback experiment
- How to read and write a single document based on MFC
- Bisenetv1 face segmentation
- Leetcode: 0-1 knapsack problem in dynamic programming [come and set the template directly]
- 箭头函数与this指向详解
猜你喜欢

Use RZ, SZ commands to upload and download files through xshell7

Latest installation tutorial of VMware Tools (rhel8)
![Theoretical basis and code implementation of dueling dqn [pytoch + pendulum-v0]](/img/f6/cbfe32991449975fe51f2e2c0c1d47.png)
Theoretical basis and code implementation of dueling dqn [pytoch + pendulum-v0]

Labelme starts normally, but cannot be opened

MySQL addition, deletion, query and modification (basic)

zsh: command not found: mysql

Powertor500t reports an error 0x01806803

Detailed explanation of arrow function and this direction
![Monte Carlo based reinforcement learning method [with code implementation]](/img/39/346b2f4122238eb0d51ca164ab6d86.png)
Monte Carlo based reinforcement learning method [with code implementation]

XX City high school network topology overall planning configuration
随机推荐
oracle 关闭回收站
Shell script receives and returns parameters
Leetcode: subsequence problem in dynamic programming
MySQL optimized index
ES6 learning notes - brother Ma at station B
洛谷每日三题之第四天
IEEE754 standard floating point format
运算符、赋值语句、结构说明语句
leetcode:50. Pow(x, n)
如何将Excel中的数据粘贴到cxGrid中
Theoretical basis of doubledqn and its code implementation [pytoch + pendulum-v0]
ES6學習筆記——B站小馬哥
CorelDRAW cannot be installed. Solution
第二章 线性表
Machine learning library scikit learn (linear model, ridge regression, insert a column of data, extract the required column, vector machine (SVM), clustering)
MySQL log management and full backup incremental backup and recovery
[MySQL] MHA high availability
通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
Monte Carlo based reinforcement learning method [with code implementation]
web语义化(强调标签-em-斜体)(重点强调标签-strong-粗体)(自定义列表:dl、dt、dd)