I included this javascript file in my php web page.
I use a if statement to see if the value of tried_or_not2 is yes.
Here is the code.
let tried_or_not2 = localStorage.getItem('tried_or_not2');
form_for_login.style['visibility'] = 'visible';
console.log('test1', localStorage.getItem('tried_or_not2'));
if (tried_or_not2 !== 'yes') {
console.log('test2');
localStorage.setItem('tried_or_not2', 'yes');
invitation_code.value = localStorage.getItem('invitation_code');
password.value = localStorage.getItem('password');
console.log('test3');
form_for_login.submit();
console.log('test4');
}
console.log('test5');
Here is the result.
https://i.stack.imgur.com/SxfB4.png
We can see test1, yes , test5 on the console.
But when I comment the line
localStorage.setItem('tried_or_not2', 'yes');
the value of tried_or_not2 becomes null.
That means the value of tried_or_not2 is created at that line ,right?
But, why does it print yes at first?
Thank you very much!
Unless you're clearing the localStorage before reloading, the previous values will persist. Meaning that even if you don't set the value now, the value you set previously will still be available. If you want the values to be cleared after the tab is closed, use sessionStorage instead. Or just clear the value after the page loads.