gpt4 book ai didi

javascript - 为什么 localStorage 不持久存在于 Chrome 中?

转载 作者:可可西里 更新时间:2023-11-01 01:17:56 25 4
gpt4 key购买 nike

我正在尝试学习如何通过以下代码使用 localStorage js 对象。

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function saveStuff() {
sessionStorage.setItem('sessionKey', 'sessionValue');
localStorage.setItem('localKey', 'localValue');
}
</script>
</head>
<body>
<button type="button" onclick="saveStuff()">Save</button>
</body>
</html>

我知道这并不总是适用于 file:///所以我使用 mongoose 来提供它。当我单击保存按钮时,Chrome 的 JavaScript 控制台中的结果对我来说是正确的。但是,当我按下刷新时,本地存储和 session 存储都会被清除,而我期望本地存储值会保留下来。这发生在 http://127.0.0.1/http://localhost/ 上。

有谁知道为什么会这样?在“设置”、“内容设置”中,我选择了“允许设置本地数据(推荐)”并且未勾选“阻止第三方 cookie 和站点数据”。我的代码中是否遗漏了什么?

(Chrome 版本 23.0.1271.64m)

最佳答案

好的。感谢必须去 pimvdb 解决这个问题,但这是解决方案。

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function saveStuff() {
sessionStorage.setItem('sessionKey', 'sessionValue');
localStorage.setItem('localKey', 'localValue');
}
localStorage.getItem('localKey');
sessionStorage.getItem('sessionKey');
</script>
</head>
<body>
<button type="button" onclick="saveStuff()">Save</button>
</body>
</html>

总结似乎是您必须尝试读取 key 才能保留它。

结论:WTF。

关于javascript - 为什么 localStorage 不持久存在于 Chrome 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13292744/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com