Help to understand localStorage.js

1

I'd like some help to enter localStorage in javascript . Objective: To make a function of saving information in the input identical to wordpress, example:

   

InthethirdprintitiswhatIwant,ifitdoesnotenteranythinginthepasswordfield,butalreadytypedinthelogin,itissavedwithoutneedingtoretype.

codethatI'musing:

<scripttype="text/javascript">

function digGet(){
    // Cria um item "usuario" com valor "Thiago Belem"
    var login = document.getElementById("user_login");
    window.localStorage.setItem('usuario', login);
    saveGet();
}

function saveGet(){
    // Depois, em outra página ou aba, recupera esse item
    var usuario = window.localStorage.getItem('usuario');
    document.getElementById('user_login').value = usuario;
}

function delGet(){

    // Remove o item
    window.localStorage.removeItem('usuario');
}
</script>

I want to transform into php this, so that the getItem ('user'); either the user typed in the input by the user.

If someone can help me, I'll be very grateful.

    
asked by anonymous 07.12.2017 / 20:24

3 answers

1

It is not recommended to autocomplete users password from the system, this should always be a user's choice.

Browsers even warn of this as a serious security breach.

If you want to keep the user's section so he does not always need to log in, it's a good choice to use

07.12.2017 / 21:08
0

Regardless of the relevance of using this, which I think has already been well explained, what was missing in your code is to run your saveGet () function when the page loads.

<script type="text/javascript">

function digGet(){
    // Cria um item "usuario" com valor "Thiago Belem"
    var login = document.getElementById("user_login");
    window.localStorage.setItem('usuario', login);
    saveGet();
}

function saveGet(){
    // Depois, em outra página ou aba, recupera esse item
    var usuario = window.localStorage.getItem('usuario');
    document.getElementById('user_login').value = usuario;
}

function delGet(){

    // Remove o item
    window.localStorage.removeItem('usuario');
}
//Rodar no load do DOM
document.addEventListener("DOMContentLoaded", function(event) { 
   saveGet();
});
</script>

Remember that the other page must be in the same domain so you can use localStorage.

    
07.12.2017 / 21:34
0

You forgot to get value of field user_login :

                                                    ↓
var login = document.getElementById("user_login").value;

In the way you did, it will return only the HTML element, not the one typed in it.

    
08.12.2017 / 01:52