How to use sessionStorage?

Asked

Viewed 2,664 times

0

I am trying to pass the value of an input from one page to fill another input on another page using HTML5 sessionStorage, I would like to know why this code is not working.

//função que captura o valor de um input na página 1
    function valor(){
        var email = document.getElementById('email');
        var emailValor = email.value;

        var teste = window.localStorage.setItem('valor', emailValor);
        console.log(emailValor);        
    }
    valor();

//função que mostra o valor em outro input na página 2
    function mostraValor(){
        var data = window.localeStorage.getItem('valor');
            document.getElementById('email').value = data;
            console.log(data);
    }   
    mostraValor();
  • what’s going wrong?

  • Pages are in the same domain?

  • Yes, they are in the same domain, I do not know what is going wrong, because the console shows no error.

3 answers

1


Hey, there’s a clerical error. tries to trade

var data = window.localeStorage.getItem('valor');

for

var data = window.localStorage.getItem('valor');
  • True Felipe, had not attacked me for the error, now it worked, but, pq the value does not appear the first time I go from page 1 to page 2 and yes only when I return to the page?

  • Leandro, if you are loading the page the first time may not have anything in the Torage and so does not display anything. I set up a small example using your code. http://exemplostorage-felipelovato99885.codeanyapp.com/index.html

  • Understood, it would be possible for the person to access the login area and fill in an input with email and click send and go to another page with an email input already filled for the first time with this localStorage?

  • Yes, you can put the script that loads the Storage data into window onload. page.

  • I tried to put window.onload = Function(){displaValor();}, but it didn’t work, tbm tried to put in the tag body: onload=showValor(); but ntbm didn’t work.

  • It worked here Felipe, haha, but once it was not working for error of writing. But I corrected and it worked, thank you very much for the help.

Show 1 more comment

-1

Try changing in the code (both in the setItem and getItem):

window.localStorage

for

sessionStorage
  • yes I have tested with localStorage, but it also didn’t work.

  • what error appears in the browser console?

  • Opa Rui has already been solved, but thank you very much for your help.

  • localStorage and sessionStorage are different things.

-3

Browser other questions tagged

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