Help to understand localStorage.js

Asked

Viewed 105 times

1

I would like a help to enter the localStorage in javascript. Objective: Make a function to save information in the input identical to wordpress, example:

- Visit http://prntscr.com/hkec1z ![- Visit http://prntscr.com/hkec5w - Visit http://prntscr.com/hkecap

In the third print is what I want, if he does not enter anything in the password field, but already typed in the login, is saved, no need to write.

code I’m using:

<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');
}
</script>

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

If anyone can help me, I’d be grateful.

  • do you want to save a user without password? and in case several people with the same name register ?

  • no, I want the guy after typing a user, stay saved in the field even though he giving F5

  • Usually browsers do this without having to use cookies or localStorage. You really think this is a relevant feature?

  • Yes, I think, because then my login system will be Perfect.

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 about this problem as a serious security breach.

If you want to keep the user section so that it doesn’t always need to log in, it is a good choice to use

  • Look, thanks for the cookies, but that’s not my need, because my system login the user who chooses to stay or leave, my need is just to save the login in the input.

0

You forgot to take the value country user_login:

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

The way it did, it will return only the HTML element, not what was typed in it.

0

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

<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 on the same domain so you can use localStorage.

  • I will be testing, in you, at first it seems very correct in this way, but I really wanted help in getItem('user'); that is, to be getItem('swanvol'); because it is in . value = user; who is giving me the account name, I would like to adjust to remove this error: [Object Htmlinputelement]

  • The answer below is on the . value that you don’t pick up when you pick up the login value, then an html object is saved and so your answer is [Object Htmlinputelement]. I believe that’s all we need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.