How do I store variables within the browser cache? (JS)

2

I am making a form and want to store the data that the client types in the browser cache, if the tab is closed the client does not have to type everything again.

What is the best way to meet this need?

    
asked by anonymous 02.10.2018 / 00:30

2 answers

3

You can use WebStorage of an object, such as a localStorage of the fields:

Imagine that you have two input fields one with ID = name and another ID = lastname you can do localstorage of these fields in this way:

var lastname = document.getElementById("lastname").value;
var name = document.getElementById("name").value;
localStorage.setItem("lastname", lastname);
localStorage.setItem("name", name);

where the first part within the parentheses represents the name of the "attribute" of localStorage, and can be referenced by localStorage.lastname / localStorage.name

    
02.10.2018 / 00:31
1

In addition to localStorage , there is also sessionStorage ( link , which turns out to be safer, since it is session-based, and complemented with an authentication system, can be time-controlled

    
02.10.2018 / 16:48