在 iPhone 上重新打开浏览器后重置本地存储值

问题描述 投票:0回答:1

我正在尝试使用本地存储,如http://www.sharonminsuk.com/code/storage-test.html 我已将 html 和 Js 复制/粘贴到我的 nginx 后端服务器上。 我也尝试过使用nodejs。

在我的智能手机上,http://www.sharonminsuk.com/code/storage-test.html,就像一个魅力。 我的没有。

当我关闭 Safari 或 Chrome 时,当我的数据丢失时,第一个数据仍然有效,当然是使用本地存储。

我尝试过 Http 和 https、Nginx 或 Nodejs 作为后端。

即使是相同的代码,一行一行! 我使用过整数、文本值,同样如此。 您是否已经预料到这种行为?您是如何解决的?

<!DOCTYPE html>

<html>

<head>
<title>Test localStorage and sessionStorage persistence</title>
</head>


<body>
<h1>Local storage:<br />
<input type="text" placeholder="nothing currently stored" size="20" id="local" />
<input type="button" onclick="store_it('local' window.localStorage);" value="Store it" />
</h1>

<h1 id="local-warning">___</h1>

<script type="text/javascript" src="/public/storage-test.js"></script>
</body>

</html>

JAVASCRIPT

function store_it(id, storageArea) {
    var textBox = document.getElementById(id);
    if (!textBox.value) {
        delete storageArea.theirValue;  // don't store empty string, delete it instead
        window.alert('5 effacé  ')
    }
    else {
        storageArea.theirValue = textBox.value;
        console.log('6')
        window.alert('Done : ' + textBox.value)
    }
}

function retrieve_storage(id, storageArea) {
    var val = storageArea.theirValue;
    window.alert('14 retrieve_storage : ' + val)
    console.log('14 ', val)
    if (val != null)
        document.getElementById(id).value = val;
}

if (window.localStorage) {
    window.alert('20 Yes')
    retrieve_storage("local", window.localStorage);
}
else {
    window.alert('24 NO')
    document.getElementById("local-warning").innerHTML = "Note: this browser does not support localStorage";
}
javascript persistence
1个回答
0
投票

但你总是可以先检查一下...

if(!localStorage.getItem('SomeVar')){

 localStorage.setItem('SomeVar', SomeValue);

}

防止刷新后覆盖。

© www.soinside.com 2019 - 2024. All rights reserved.