刷新浏览器后,sessionStorage消失了-Javascript [英] sessionStorage is gone when browser is refreshed - Javascript
问题描述
我试图在sessionStorage中保留一些数据,但是如果刷新页面或离开链接然后返回,则sessionStorage不再存在.
I am trying to keep some data in sessionStorage, but if I refresh the page or leave from a link then come back, the sessionStorage no longer exists.
我是sessionStorage的新手,如果很明显,对不起.
I am new to sessionStorage, so sorry if this is an obvious fix.
基本上,我将一个数组存储到sessionStorage中.
Essentially I store an array into the sessionStorage.
$scope.addPlant = function(plant) {
for (i = 0; i < $scope.userPlantList.length; i++) {
if ($scope.userPlantList[i] === plant) {
alert("You have already added this plant");
return;
}
}
$scope.userPlantList.push($scope.currentPlant);
sessionStorage.setItem("Plants",JSON.stringify($scope.userPlantList));
};
然后当我想查看所有存储的内容
And then when I want to see what is all stored
$scope.retreiveList = function() {
var retrieved = sessionStorage.getItem("Plants");
$scope.userPlantList = JSON.parse(retrieved);
}
当我根本不刷新页面/应用程序时,这很好用.
And this works fine when I do not refresh the page/app at all.
问题:如何在刷新/立即重新访问期间使sessionStorage持续使用?
Question: How can I get my sessionStorage to last during a refresh/immediate re-visit?
推荐答案
通过查看开发者工具检查数据是否真的消失了
Check if the data are really gone by looking at the developer tools
如果您使用的是chrome:F12->资源"标签->会话存储".
If you using chrome: F12 -> Resources tab -> Session Storage.
sessionStorage与浏览器"选项卡一起使用.每当您关闭标签页时,sessionstorage数据就会被清除.
sessionStorage lives with the browser tab. whenever you close a tab the sessionstorage data will be wiped out.
如果您希望在标签之间共享某些内容,请查找localStorage.
if you want something that will be shared across tabs, look for localStorage.
这篇关于刷新浏览器后,sessionStorage消失了-Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!