1

Working on a Chrome Extension that allows users to keep a list that uses localStorage.

Working so far: user can input things in their list and it will store in localStorage, user can delete things from the list and it disappears from localStorage.

Trying to figure out: how can I start the user with a few sample items on their list?

function startStorage (){
localStorage.setItem('list', 'your first list item');}
startStorage();

The above code's issue: every time the user refreshes the page it would populate the starting items again. Is there a way to populate only once? As of now, function fires everytime they reload so they get the starting items again.

Any thoughts are appreciated, thanks.

H Roark
  • 65
  • 4

1 Answers1

1

You can check if this local storage item already exist or not with getItem

function startStorage (){
  if(!localStorage.getItem('list')){
  localStorage.setItem('list', 'your first list item')
   }
}
startStorage();
Evren
  • 4,147
  • 1
  • 9
  • 16