Storing prompt response in localStorage and retrieving
This question already has an answer here:
Well for one, person
is always going to be prompted since you aren't checking localStorage.getItem
before prompting. Second, you aren't actually setting anything with localStorage.setItem
since you don't give a second argument.
You may want something like the following:
var username = localStorage.getItem("username");
if(!username){
username = prompt("Please enter username");
localStorage.setItem("username", username);
}
Be aware that like in your usage of setItem
, JavaScript will not warn you or have an error if arguments are not supplied to a function call. Be sure to check that you're actually passing what is necessary.
You'll also want to note that not all browsers will support localStorage. You may want to read up on some questions such as this one to look at some of the other differences between localStorage and cookies. One of the big ones is that localStorage is client-side while cookies are server-side.
链接地址: http://www.djcxy.com/p/27946.html上一篇: 为什么控制台打印数组中的对象而不是对象