开发者

javascript welcoming msgbox problem

i'm doing my html and javascript class project

and i want to have a msgbox welcoming the visitor asking for his name and then a welcoming msgbox shows up saying "hello (the name added)"

i know how to do that but my only problem is that if i pressed back and went to the homepage of my website again, the msg will show up again asking for my name

is there anyway to prevent this from happening? serio开发者_运维技巧usly don't want the visitor to wite his name each time he goes to the home page

please help

this is the code i'm using:

     <script> 
response = window.prompt("Welcome!","Please enter your name");


window.alert("hello " + response);




</script>

please help


The script will aways execute the same way. When you tell it to show a message box every time the page is loaded, regardless of how often it has been done in the past, it WILL happen again and again and again and again.

You could save the name in a cookie, and before prompting anything just check if there's already a cookie with the name. If there is, use that one. If there isn't, ask for one, use it and save it in a cookie. How to do this is covered basically everywhere, try googling.

The worse problem I see here is the whole prompting and message boxing. Don't ever do that. If you ever had any visitor on that site, they will never be coming back. Don't do such things with Javascript. It's like you are running through the city, grabbing every person you see, hold them, ask them for their name, YELL it in their FACE and go do the same with the next person. It's all fun and games until someone looses an eye.


The example given on W3Schools for using cookies is exactly what you need.

http://www.w3schools.com/js/js_cookies.asp

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜