JavaScript 网络存储
当然!这是 JavaScript Web 存储和会话存储的备忘单:
网络存储:
Web Storage 提供了两个用于在客户端存储数据的对象:localStorage和sessionStorage。
和localStorage都sessionStorage具有相同的 API 和属性,但它们在持久性和范围方面有所不同。
存储在 Web Storage 中的数据被保存为键值对,其中键和值都是字符串。
本地存储:
localStorage即使浏览器关闭并重新打开,存储在中的数据仍然存在。
它没有到期日期,并且会一直存储到明确清除为止。
要将数据存储在 中localStorage,请使用该setItem(key, value)方法。例子:localStorage.setItem(‘username’, ‘John’)。
要从 检索数据localStorage,请使用getItem(key)方法。例子:const username = localStorage.getItem(‘username’)。
要从 中删除项目localStorage,请使用removeItem(key)方法。例子:localStorage.removeItem(‘username’)。
要清除存储在 中的所有数据localStorage,请使用clear()方法。例子:localStorage.clear()。
会话存储:
存储在中的数据sessionStorage仅在当前浏览器会话期间可用。
当浏览器窗口/选项卡关闭时,数据将被清除并且无法再访问。
sessionStorage方法(setItem()、getItem()、removeItem()和clear())的使用方式与 相同localStorag
还没有评论,来说两句吧...