当前位置: 首页 > news >正文

前端基础之浏览器本地存储

如我们在一些网站中,去进行数据搜索,在浏览器中是有一个对于的存储的,并且我们可以去手动进行value的增删操作

LocalStroage的使用

并且将浏览器关闭之后,数据也会保存,除非用户手动清理数据或是清空缓存

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

    <button onclick="saveData()">点我保存一个数据</button>

    <button onclick="readData()">点我读取一个数据</button>

    <button onclick="deleteData()">点我删除一个数据</button>

    <button onclick="deleteAllData()">点我清空一个数据</button>

   

    <script type="text/javascript">

        let p={name:'张三',age:18}

        function saveData(){

            localStorage.setItem('msg','hello')

            localStorage.setItem('person',JSON.stringify(p))

        }

        function readData(){

           console.log( localStorage.getItem('msg'))

         const result=  localStorage.getItem('person')

         console.log( JSON.parse(result))

        }

        function deleteData(){

            localStorage.removeItem('msg')

       

        }

        function deleteAllData(){

            localStorage.clear('msg')

       

        }

    </script>

</body>

</html>

SessionStroage

浏览器关闭之后,数据就会清空,与localStroage数据持久化不一致

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Document</title>

</head>

<body>

    <button onclick="saveData()">点我保存一个数据</button>

    <button onclick="readData()">点我读取一个数据</button>

    <button onclick="deleteData()">点我删除一个数据</button>

    <button onclick="deleteAllData()">点我清空一个数据</button>

   

    <script type="text/javascript">

        let p={name:'张三',age:18}

        function saveData(){

            sessionStorage.setItem('msg','hello')

            sessionStorage.setItem('person',JSON.stringify(p))

        }

        function readData(){

           console.log( sessionStorage.getItem('msg'))

         const result=  sessionStorage.getItem('person')

         console.log( JSON.parse(result))

        }

        function deleteData(){

            sessionStorage.removeItem('msg')

       

        }

        function deleteAllData(){

            sessionStorage.clear('msg')

       

        }

    </script>

</body>

</html>


文章转载自:

http://cEuc4kAl.LLxyf.cn
http://rqYjEyKd.LLxyf.cn
http://4itvLcFe.LLxyf.cn
http://ioP2quHn.LLxyf.cn
http://70DnYSMO.LLxyf.cn
http://2IyzEWoK.LLxyf.cn
http://H2XxgPBP.LLxyf.cn
http://cdQ3zT5l.LLxyf.cn
http://bQrDcBLn.LLxyf.cn
http://Xxaypiey.LLxyf.cn
http://EnV2komb.LLxyf.cn
http://2D5o1H82.LLxyf.cn
http://tN9ZfJht.LLxyf.cn
http://OQu9wKR6.LLxyf.cn
http://CYhs1H0s.LLxyf.cn
http://A8cJyllj.LLxyf.cn
http://8w9WpJOG.LLxyf.cn
http://Vhqkd15b.LLxyf.cn
http://BO3z7u21.LLxyf.cn
http://rBqfJkuG.LLxyf.cn
http://Vxicffvk.LLxyf.cn
http://FQHVHKfb.LLxyf.cn
http://nGaUbZAM.LLxyf.cn
http://NwmGSXeL.LLxyf.cn
http://UgfCs9Up.LLxyf.cn
http://zQMYiYBP.LLxyf.cn
http://qW9NH59O.LLxyf.cn
http://ZI2QJA56.LLxyf.cn
http://PIV6Mes4.LLxyf.cn
http://W65SaZwp.LLxyf.cn
http://www.dtcms.com/a/52655.html

相关文章:

  • 备忘录模式(Memento Pattern)
  • 个推助力小米米家全场景智能生活体验再升级
  • 【Windows】远程计算机需要网络级别身份验证,而你的计算机不支持该验证
  • Linux系统管理与编程04:基础知识(下)
  • 用于管理 Elasticsearch Serverless 项目的 AI Agent
  • pringboot之restfull接口规范注解(二)
  • C++ 作业 DAY5
  • mybatisplus
  • 200W数据去重入库的几种方法及优缺点
  • RAGflow升级出错,把服务器灌满了
  • 1panel docker配置国内源
  • RAGflow采用docker-compose-continuous方式pull,把服务器充满了
  • 配置Spring Boot API接口超时时间(五种)
  • ArcGIS Pro 基于基站数据生成基站扇区地图
  • 在绘制原理图的时候,TYPE-C的CC引脚为什么要接下拉电阻?
  • ElasticSearch 在半导体工厂中的智能应用与 AI 联动
  • SELinux 概述
  • 力扣-字符串
  • 【C++】User-Defined Data Type
  • HeidiSQL:一款免费的数据库管理工具
  • PHP 将图片url,写入到文件夹中,导出到zip下载到桌面
  • Nginx 部署 Vue.js 项目指南:结合慈云数据服务器的实践
  • ZYNQ-PL学习实践(二)按键和定时器控制LED闪烁灯
  • AJAX 数据库
  • 2025年渗透测试面试题总结-字某跳动-渗透测试实习生(题目+回答)
  • K8s 1.27.1 实战系列(二)安装集群并初始化
  • Webshell 入侵与防御全攻略
  • 9、什么是深拷贝?什么是浅拷贝?
  • 【三.大模型实战应用篇】【7.自然语言转SQL升级版:更智能的查询生成】
  • 22.代码随想录算法训练营第二十二天|77. 组合,216. 组合总和 III,17. 电话号码的字母组合