最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - localstorage values resetting after refresh - Stack Overflow

programmeradmin5浏览0评论

i want to have an array (converted also to a localStorage value) box where the value of '#textInput' is added to the localStorage each time, however after refreshing and entering a new value, the new value is not added to the localStorage, but replaces it. I believe this is because after refreshing the data from users[] is set to null, but dont know how to fix it

var users = new Array();
var valueChanger = 1;

alert(localStorage.getItem('usernames'));

function addValue() {
  var newUser = $('#textInput').val();
  users.push(newUser);
  localStorage.setItem('usernames', users);
  alert(localStorage.getItem('usernames'));
}

i want to have an array (converted also to a localStorage value) box where the value of '#textInput' is added to the localStorage each time, however after refreshing and entering a new value, the new value is not added to the localStorage, but replaces it. I believe this is because after refreshing the data from users[] is set to null, but dont know how to fix it

var users = new Array();
var valueChanger = 1;

alert(localStorage.getItem('usernames'));

function addValue() {
  var newUser = $('#textInput').val();
  users.push(newUser);
  localStorage.setItem('usernames', users);
  alert(localStorage.getItem('usernames'));
}
Share Improve this question edited Feb 26, 2016 at 18:29 Andy 63.6k13 gold badges71 silver badges98 bronze badges asked Feb 26, 2016 at 18:28 igetstuckalotigetstuckalot 2373 gold badges8 silver badges16 bronze badges 2
  • 2 localStorage only saves strings, so you have to save and load in a special way non strings. json.stringify and json.parse is monly used – juvian Commented Feb 26, 2016 at 18:31
  • What Juvian said and you also never retrieve the value to a variable. Users is initialized to a new array every page load. You need to stringify the value when setting it. Parse it and set it to a variable on getting. Then you can push a new value to it. – AtheistP3ace Commented Feb 26, 2016 at 19:21
Add a ment  | 

3 Answers 3

Reset to default 7

You have to rewrite your code like the below to make it working,

var users = JSON.parse(localStorage.getItem('usernames')) || [];
var valueChanger = 1;

alert(localStorage.getItem('usernames'));

function addValue() {
  users.push($('#textInput').val());
  localStorage.setItem('usernames', JSON.stringify(users));
  alert(localStorage.getItem('usernames'));
}

Since localStorage only stores string, we need to stringify the array before storing and Parse it before reading.

Everytime, when page refreshes you're first statement creates new array instead of using old one.

var newUser = localStorage.getItem('usernames') || "";
var valueChanger = 1;
var users = [newUser];

alert(localStorage.getItem('usernames'));

function addValue() {
  var newUser = $('#textInput').val();
  users.push(newUser);
  localStorage.setItem('usernames', users);
  alert(localStorage.getItem('usernames'));
}

Thanks & Cheers :)

You are correct in that users is an empty array each time you refresh. Instead of using the new array "users" in your function. Grab the information (if any) out of localStorage.setItem('usernames', users); and set that to equal users. Then you can push additional users into usernames.

发布评论

评论列表(0)

  1. 暂无评论