admin管理员组

文章数量:1392002

This is my code. I'm trying to alert the myname variable value after saving it in localStorage, but instead of getting the variable's saved value, I only get the variable name in the alert.

What am I doing wrong?

Thanks in advance.

$(document).ready(function(){
  var myname = prompt("What is your name, friend?");
  localStorage.setItem('username', 'myname');
  localStorage.getItem('username');
  alert('username');
});

This is my code. I'm trying to alert the myname variable value after saving it in localStorage, but instead of getting the variable's saved value, I only get the variable name in the alert.

What am I doing wrong?

Thanks in advance.

$(document).ready(function(){
  var myname = prompt("What is your name, friend?");
  localStorage.setItem('username', 'myname');
  localStorage.getItem('username');
  alert('username');
});
Share Improve this question edited Sep 19, 2017 at 9:03 Shiladitya 12.2k17 gold badges28 silver badges42 bronze badges asked Sep 19, 2017 at 9:00 Rocky RacoonRocky Racoon 751 silver badge10 bronze badges 2
  • “What am I doing wrong?” - you are alerting a static text. And you call getItem without doing anything with the result. – C3roe Commented Sep 19, 2017 at 9:03
  • Please check the answers below, update your code that as well. don't put strings everywhere when you want to use vars. – bhansa Commented Sep 19, 2017 at 9:15
Add a ment  | 

7 Answers 7

Reset to default 3

Your final code would be:

$(document).ready(function(){
  var myname = prompt("What is your name, friend?");
  localStorage.setItem('username', myname);
  var username = localStorage.getItem('username');
  alert(username);
});


Why:

Surely username is not defined anywhere, it is just a string and you are putting a alert on it.

Also you are not setting your prompt value in setItem() either.

Either store your getItem value in a variable

var username = localStorage.getItem('username');

Or alert this one

localStorage.getItem('username');

Use alert(localStorage.getItem('username'))

Change alert('username') to alert(localStorage.getItem('username'))

You put the localStorage.getItem-result into a variable.

$(document).ready(function(){
    var myname = prompt("What is your name, friend?");
    localStorage.setItem('username', 'myname');
    var username = localStorage.getItem('username');
    alert(username);
});

getItem() returns a value, you get set it into a variable

    $(document).ready(function(){
    var myname = prompt("What is your name, friend?");
    localStorage.setItem('username', myname);
    var user = localStorage.getItem('username');
    alert(user);
    });

The reason that you only get the variable name inside the alert is that you are just giving a string to the setItem that is the same as your variable name.

$(document).ready(function(){
    var myname = prompt("What is your name, friend?");
    localStorage.setItem('username', myname);
    var item = localStorage.getItem('username');
    alert(item);
    });

The way you can fix this is just changing the 'myname' into a the variable myname

You are trying to alert a variable i.e. not defined.

Variable username.

$(document).ready(function(){
  var myname = prompt("What is your name, friend?");
  localStorage.setItem('username', 'myname');
  var username = localStorage.getItem('username');
  alert('username');
});

Hope this will help you.

本文标签: javascriptHow to get localstorage to alert the prompted username variableStack Overflow