Skip to content Skip to sidebar Skip to footer

What Is Localstorage?

Could someone please explain to me what localStorage is, and how or where I would use it? I realise this question can be answered by 'Googling', but I prefer the concise, easy to f

Solution 1:

localStorage is a way to store data on the client's computer. Let's say you want to save the date the last time a user visited your webpage. You can use the following code once the page loads:

functionsaveData() {
  localStorage.lasttimevisited = newDate();
}

The next time the page loads you can check if localStorage.lasttimevisited is full and if it is, welcome the user.

The advantages to localStorage is that it will still be in memory even when you close the browser. If somebody visits that page, they will get a welcome from last time (if they visited last time) even if the last time they visited was centuries ago.

However, there are some disadvantages. The user can clear the browser data/cache to erase all localStorage data. They can also have an unsupported browser, such as IE7.

localStorage can only be accessed via JavaScript, and is HTML5.

Post a Comment for "What Is Localstorage?"