localStorage utility

1

I wanted to save a series of user data, but not use a database. I have seen the existence of localstorage but I do not finish understanding it.

If the user writes something and I store it with localstorage . If tomorrow opens the page again, what you wrote is still there? Or is it just refreshing the page?

    
asked by NEA 03.01.2018 в 14:24
source

1 answer

5

Introduction:

The localStorage property allows you to access the local object Storage . localStorage is similar to sessionStorage . The only difference is that, while the data stored in localStorage do not have an expiration date, the data stored in sessionStorage is deleted when the browsing session ends - which happens when the browser is closed.

With sessionStorage the data persists only in the window / tab that created them, while with localStorage the data persists between windows / tabs with the same origin.

Advantages:

  • LocalStorage can occupy between 5 and 10MB depending on the web browser.
  • There is no expiration for localStorage , the information will remain Stored until it is expressly deleted. Even if the browser.

Disadvantages:

(Limitation)

  • Only text strings can be stored. That is, it can not be saved boolean (true or false), nor save arrays, objects, floats ... only strings.

Solution: The browsers that support localStorage (that is, the modern ones) also have support for JSON. Thanks to JSON you can convert an object (or whatever that) into text string and store it in the localStorage.

Examples:

localStorage.setItem('miGato', 'Juan'); // Guardar Parametro

localStorage.setItem('objectKey', JSON.stringify(object)); //Guardar un objeto

var miGatoName = localStorage.getItem('miGato'); // Obtener Parametro

var object = JSON.parse(localStorage.getItem('obcjetKey')) //Objetener un objeto

localStorage.removeItem('miGato'); //Eliminar Parametro

localStorage.clear(); // Limpia todo el Store
    
answered by 03.01.2018 / 14:38
source