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