javascript - 在浏览器上临时存储数据
问题描述
我们可以在浏览器上临时存储数据吗?例如,我有页面 A 和页面 B
在页面 A(a.html) 上,我想在按钮单击时临时存储文本“Helloworld”
function a(){
return "helloWorld"
}
function changePage(){
a()
window.location(b.html)
}
在页面 B(B.html) 上,我想在页面 A 上捕获“Helloworld”并将其打印在页面 b 上。
console.log(a())
我在 postMessage 上阅读,但根据我的理解,这仅适用于跨域解决方法的单个页面上加载的 2 个页面。
解决方案
您可以使用浏览器的本地存储功能:localStorage。它有助于将数据作为字符串存储在浏览器上。
存储数据:
localStorage.setItem('foo', 'helloWorld');
要获取保存的数据:
localStorage.getItem('foo');// return 'helloWorld'
推荐阅读
- python - 张量板有两个一维图而不是一个二维图的问题
- node.js - 使用 Node js 将 Angular 应用程序作为 docker 镜像运行
- javascript - 在测验中创建一个像视频游戏一样的生命系统
- javascript - 在 Google Chrome 控制台中,如何查看标签的修改位置和方式?
- javascript - 如何在 JavaScript 中检测 iOS 模拟器
- excel - 如何使用单元格内容作为 Excel VBA 中范围公式的输入?
- python - 在熊猫中连接并保存多对CSV
- android - 从 Android Studio 中提取的 adb 和数据库未在 SQLiteDatabaseBrowser 3.10.1 中更新
- android - ViewModelProviders 实例化失败,因为“of”无法解决
- python - 使用 Python 从 Linux 核心服务器下载经过身份验证的 Django 应用程序的文件