views:

38

answers:

2

hey... 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? seriously 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

+3  A: 

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

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

ChaosPandion
thanks :) i got it.. really thanks, i'm going to talk about it in the class thanks all for help great site really
shouq
+3  A: 

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.

LukeN
thanks alot ^_^i'm going to follow your advice and not ask for a name.. you are right.thanks alot really :)
shouq
I have to be honest, I was debating whether to write a similar answer.
ChaosPandion
If you want to have some kind of remembering system, you could always do it with a server sided scripting, which will also allow for permanent storage with a whole account associated with it - should there be a need for it :)
LukeN
@ChaosPandion: our answers are completing each other anyways. I have the moral "don't ever do that" part, and you have the help on cookies!
LukeN
@Luke - That is a good way to look at it.
ChaosPandion