我已经为一些对用户体验有用的数据实现了localStorage,但对于应用程序本身不是必不可少的(不会破坏功能)。
问题:每次我进入显示页面,然后按“返回”按钮进入“索引”页面时,我所有的本地存储都消失了。
使用以下功能在INDEX页中设置localStorage:
$(document).ready(function () {
return $(".sort_link").on("ajax:success", function(e, data, status, xhr) {
localStorage.removeItem('results'); //removes any old localStorage values
if (document.getElementById("results-dupl")) {
var results = $("#results-dupl").html();
localStorage.results = results
} else {
var results = $("#searc_re").html();
localStorage.results = results
}
}).on("ajax:error", function(e, xhr, status, error) {
});
});
基本上,localStorage密钥是独立设置的,而不是在离开当前INDEX页之前进行设置。
如果我按任何其他重定向到INDEX页面的链接,则不会出现问题。我已经尝试过,并且在Firefox和Chrome上也会出现相同的问题,所以我猜-在所有浏览器上。
我尝试过的是:
1)localStorage.getItem('results');
在设置localStorage密钥后立即添加,如其他问题所示。
2)仔细检查上述功能是否实际创建了localStorage密钥-是的,100%创建了。
3)似乎在访问显示页面之后,然后使用“ BACK”按钮返回到INDEX页面,丢失了在同一INDEX页面中创建的cookie。
在此先感谢您的帮助。
编辑:
根据接受的答案找到了解决方案。
基本上,以前以某种方式注释掉了脚本正在运行,并删除了localStorage和cookie:
localStorage.removeItem('menu');
localStorage.removeItem('results');
document.cookie = "menu_storage=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
不要使用localStorage.removeItem('results');
。它可以解决这个问题,而且localStorage.removeItem('results');
在这种情况下仍然是无用的,因为您可以编辑localStorage而不是删除它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句