首页 > 解决方案 > 在浏览器上临时存储数据

问题描述

我们可以在浏览器上临时存储数据吗?例如,我有页面 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 个页面。

标签: javascript

解决方案


您可以使用浏览器的本地存储功能:localStorage。它有助于将数据作为字符串存储在浏览器上。

存储数据:

localStorage.setItem('foo', 'helloWorld');

要获取保存的数据:

localStorage.getItem('foo');// return 'helloWorld'

推荐阅读