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
7 Answers
Reset to default 3Your 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
版权声明:本文标题:javascript - How to get localstorage to alert the prompted username variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744774441a2624539.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论