LocalStorage does not keep the data saved when the browser is closed


I'm trying LocalStorage to store data in the client's memory and I have a problem that I can not solve.

I save an object transformed into a chain by means of setItem in LocalStorage and I can access it by refreshing the browser (Chrome 49.0) quietly. The problem comes when I close the browser the data is not preserved.


'use strict';

var obj           = {}; // Creamos un Obj en ambito global
var objsOnArray   = []; // Creamos un Array en ambito global
var arrayOnString = ''; // Creamos un String en ambito global

// Recibimos los valores del formulario
function save( uno, dos ) {

  // Los valores los agregamos al Obj
  obj = {
    "uno": uno.value,
    "dos": dos.value

  // El Obj lo añadimos a un array que va a contener los objetos
  objsOnArray.push( obj );
  console.log( objsOnArray ); // Array[Obj, Obj, ...]

  // Transformamos este array de objetos a una cadena de texto para guardarla en local
  arrayOnString = JSON.stringify( objsOnArray );
  console.log( arrayOnString ); // String

  // Lo mandamos al LocalStorage
  localStorage.setItem( "all", arrayOnString );
  console.log( localStorage ); // String

  // Devolvemos el Obj para que se almacene en el ámbito global
  return objsOnArray;


function getLocal() {

  // accedemos a All en LocalStorage y lo reconvertimos en Obj
  objsOnArray = JSON.parse( localStorage.getItem("all") );
  console.log( objsOnArray ); // Obj

  // Devolvemos el Obj para almacenarlo en el ámbito global
  return objsOnArray;



<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

<form name="form">

  <input name="uno" type="text">
  <input name="dos" type="text">

  <input type="button" value="Enviar" onclick="save( uno, dos )">


<script src="functions.js"></script>
<script src="launch.js"></script>



// Cuando esté cargado ejecuta...
window.onload = function() {

  // Si hay datos almacenados al abrír el navegador, muestralos
  if( localStorage.length > 0 ) {

    console.log('ONLOAD >>> Ya había datos en LocalStorage');

    return getLocal();

  } else {

    return console.log('ONLOAD >>> NO había ningún dato');



Does anyone know how to fix it?

asked by Pablo García 20.03.2016 в 23:56

1 answer


This can be a problem with the browser settings. If you have it set so that cookies are deleted when you close the browser, the LocalStorage will also be deleted.

To verify that they are not disabled by default, or that they will not be deleted when the browser is closed:

  • Go to the Chrome settings ( settings ),
  • From there go to the privacy settings ( privacy )
  • Click on the Content Settings button ... ( content settings ... )
  • Make sure that the first checkbox is the one marked:

    Otherwise, LocalStorage will not be saved, because browsers should treat it in the same way as cookies ( source ).

  • answered by 21.03.2016 / 04:21